E
E
ekolodenets2020-07-07 10:57:08
Python
ekolodenets, 2020-07-07 10:57:08

Python. Iterating over unique values ​​skips values?

Iteration skips 'two' as unique (even though it's not in the checklist) and leaves it for later

def list(lst):
    lst_new = []
    while lst:
        lst_temp = []
        for i in lst:
            if i not in lst_temp:
                lst_temp.append(i)
                lst.remove(i)
        lst_new += lst_temp
    return lst_new

l = list(["one", "one", "two", "two", "three", "three", "four", "one"])
d = ["one", "two", "three", "four", "one", "two", "three", "one"] #должно быть так
print(l)
print(d)

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Sergey Gornostaev, 2020-07-07
@ekolodenets

Because it is not necessary to change the iterable collection.

S
soremix, 2020-07-07
@SoreMix

You read the list and change it at the same time.
In general, you have a strange design.

B
bbkmzzzz, 2020-07-07
@bbkmzzzz

Do not use names that are the same as built-in types.
Changing the iterated list is also a bad idea, when an element is removed, the remaining elements change their position, but the loop takes the next element, regardless of whether the order has changed or not, that's why there are gaps.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question