[Python] When creating a list with unique elements from a list with duplicated elements, why...

Discussão em 'Python' iniciado por Stack, Fevereiro 1, 2025.

  1. Stack

    Stack Membro Participativo

    I want to create a list unique_list that has elements from my_list without the duplicates.

    Output should be:

    if my_list = [1, 2, 1, 5, 2, 4, 3, 7, 2, 5]
    then unique_list = [1, 2, 5, 4, 3, 7]


    The code I tried using:

    unique_list = []
    unique_list = [element for element in my_list if element not in unique_list]


    This didn't work and it created a unique_list with all the duplicate elements included (basically a copy of my_list)

    I have already found a solution:

    unique_list = []
    [unique_list.append(element) for element in my_list if not (element in unique_list)]


    And this solution:

    for element in my_list:
    if element not in unique_list:
    unique_list.append(element)


    I'm trying to understand why using the target list in a list comprehension's condition doesn't work.

    Since the for loop solution works, I thought "translating" it into a list comprehension would also work, but it doesn't.

    Why does it only work when using .append after defining the list but not when defining unique_list with list comprehension? And why does using the target list unique_list in the for loop condition work but not when using it in the list comprehension condition?

    The thing that I tried to do to understand this (after learning that list comprehensions don't leak elements into the enclosing scope) is

    unique_list = [5]
    unique_list = [element for element in my_list if element not in unique_list]


    By assigning 5 to unique_list, the new unique_list won't include any 5s from my_list. So that means the condition in the list comprehension checks if elements are in the old unique_list (the one containing 5). So does list comprehension not use the target list unique_list as it's being updated?

    This is how I visualize the list creation process with the two solutions that worked:

    element '5' is not in unique_list ([])
    element '5' is added to unique_list

    element '2' is not in unique_list ([5])
    element '2' is added to unique_list

    element '7' is not in unique_list ([5, 2])
    element '7' is added to unique_list

    element '5' is already in unique_list ([5, 2, 7])
    element '5' is not added to unique_list

    final result: unique_list = [5, 2, 7]


    Is this not the case for list comprehension that uses the target list?

    Continue reading...

Compartilhe esta Página