V
V
Val_Mi2020-06-30 15:24:22
Python
Val_Mi, 2020-06-30 15:24:22

How to remove duplicates from a list with datatype checking?

there is this code

def clean_list(l):
result = []
for item in l:
if item not in result:
result.appent(item)
return result
only it doesn't distinguish float from int it doesn't reach me where to do type checking

Answer the question

In order to leave comments, you need to log in

4 answer(s)
V
Vladimir Kuts, 2020-06-30
@Val_Mi

If I understand the question correctly, then:

def clean_list(a):
    out = []
    for im in a:
        tmp = [im, type(im)]
        if not tmp in out:
            out.append(tmp)
    return [x[0] for x in out]


print(clean_list([1, 1, 2.0, 2, 2.0, 3, 1, 2, 3.0, 3.0, 2.0, 4, 4.0, 3.0]))

[1, 2.0, 2, 3, 3.0, 4, 4.0]

M
meaqese, 2020-06-30
@meaqese

clean_list = lambda l: list(set(l))

S
soremix, 2020-06-30
@SoreMix

1. What was wrong with set()?
2. ?
def clean_list(l):
result = []
for item in l:
if int(item) not in result:
result.appent(int(item))
return result

A
aRegius, 2020-06-30
@aRegius

unique_as_tuple = dict.fromkeys([(num, type(num)) for num in values_list])
unique_as_numbers = [num for num, num_type in unique_as_tuple]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question