[Python] How do I remove duplicates from a list in Python while preserving order? [closed]

Discussão em 'Python' iniciado por Stack, Junho 24, 2025.

  1. Stack

    Stack Membro Participativo

    Absolutely — here’s a detailed body for that short question, expanded to at least 300 words in a clear, Stack Overflow–friendly style:


    Body: I am trying to remove duplicate elements from a Python list while making sure that the original order of the elements is preserved. I have read that using a set can remove duplicates, but since sets are unordered collections, this approach does not guarantee that the result will keep the same order as the original list. For example, if I have a list like [1, 2, 2, 3, 1], I want the output to be [1, 2, 3]. The important point for me is that the first occurrence of each element should be kept, and any subsequent duplicates should be removed, without changing the order in which the unique elements appear.

    Here is a simple example to illustrate my problem:

    my_list = [1, 2, 2, 3, 1]
    # Desired output: [1, 2, 3]


    I tried the following approach using set():

    my_list = [1, 2, 2, 3, 1]
    result = list(set(my_list))
    print(result)


    But when I run this code, I get something like [1, 2, 3], but the order may vary because sets do not maintain insertion order in versions of Python before 3.7. I am currently working with Python 3.6 and 3.10 on different systems, so I would prefer a solution that works consistently across both. I also want to understand if there is an efficient or idiomatic way to do this in Python, perhaps using list comprehensions, dict.fromkeys(), or some module from the standard library like collections.

    I also read that in Python 3.7 and later, dictionaries maintain insertion order by specification, so maybe there is a neat trick using dict.fromkeys(). However, I am not entirely sure if this is the recommended or fastest way, or if it works well for lists with non-hashable elements like lists of lists (e.g., [[1], [2], [1]]). In my case, my lists only contain hashable types like integers, strings, or tuples, so I do not need to handle unhashable types at the moment.

    Can someone please explain what is the best practice for removing duplicates while preserving the original order? I am also curious if there are any performance considerations when working with large lists containing tens of thousands of elements. Should I use a loop, a list comprehension with a set to track seen items, or some other method? It would be great if you could provide a short, idiomatic example along with any caveats or tips. Thank you!

    Continue reading...

Compartilhe esta Página