1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

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

Discussão em 'Python' iniciado por Stack, Setembro 2, 2025.

  1. Stack

    Stack Membro Participativo

    I have a list in Python that contains duplicate elements. I want to remove the duplicates but still keep the original order of the items.

    my_list = ["apple", "banana", "apple", "orange", "banana", "kiwi"]

    I used set(my_list), but that removes duplicates without preserving the original order. I also looked into using dict.fromkeys(), but I’m not sure if that’s the most efficient or Pythonic way.

    I expected: ["apple", "banana", "orange", "kiwi"]

    Continue reading...

Compartilhe esta Página