When you remove a number and add a zero to the end of the list, your main loop (for i ...) will eventually get to the zeroes at the end. At that point you will have removed one '1' and two '5' so the count will be 3 and there will be 3 zeroes to process. Each of these zeroes will find two other zeroes in the list (replacement with another zero wont make a difference). So, in total 1+2+2+2+2 = 9 counting 1 (for the 1's) 2 (for the 5's) and three more times 2 (for each of the zeroes).

One way around this (without changing your code too much) would be to break the main loop when you reach a zero (assuming zero is not a legitimate value in the initial list)

Answer from Alain T. on Stack Overflow
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-ways-to-remove-duplicates-from-list
Remove Duplicates from a List in Python - GeeksforGeeks
This method removes duplicates by converting the list into a set, which only stores unique values. ... This method checks each element and adds it to a new list only if it is not already present using for loop.
Published ย  December 20, 2025
Discussions

Python: Iterate through list and remove duplicates (without using Set()) - Stack Overflow
So I have a list: s = ['cat','dog','cat','mouse','dog'] And I want to be able to iterate through the list and remove the duplicates, WITHOUT using the set() function! So for example it should remo... More on stackoverflow.com
๐ŸŒ stackoverflow.com
python - Removing duplicates in lists - Stack Overflow
How can I check if a list has any duplicates and return a new list without duplicates? More on stackoverflow.com
๐ŸŒ stackoverflow.com
How to quickly remove duplicates from a list?

Good points!

Try to avoid posting code as images - it's bad when it comes to accessibility (bad eyesight, etc.), it's bad when it comes to copyability (although it's nice that we're able to emulate the 80s of typing in code from magazines) and it doesn't really work for searching.

More on reddit.com
๐ŸŒ r/Python
197
2741
October 22, 2020
Is this a more efficient way of removing duplicates?
โ€˜new_list = list(set(old_list))โ€™ would be pretty clean and probably quite efficient. Not sure what the set constructors time complexity is, but worse than O(n) would surprise me. More on reddit.com
๐ŸŒ r/learnpython
25
8
December 6, 2023
๐ŸŒ
Dataquest
dataquest.io โ€บ home โ€บ blog โ€บ how to easily remove duplicates from a python list
How to Easily Remove Duplicates from a Python List โ€“ Dataquest
May 12, 2025 - To remove duplicates using for-loop, first you create a new empty list. Then, you iterate over the elements in the list containing duplicates and append only the first occurrence of each element in the new list.
๐ŸŒ
Switowski
switowski.com โ€บ blog โ€บ remove-duplicates
Remove Duplicates From a List - Sebastian Witowski
October 22, 2020 - It's 62% slower than using a set (17.9/11โ‰ˆ1.627), but still over 30 times faster than the "for loop" (634/17.3โ‰ˆ35.419). The above method only works with Python 3.6 and above. If you are using an older version of Python, replace dict with OrderedDict: # duplicates.py from collections import OrderedDict def test_ordereddict(): return list(OrderedDict.fromkeys(DUPLICATES))
๐ŸŒ
The Python Coding Stack
thepythoncodingstack.com โ€บ the python coding stack โ€บ i want to remove duplicates from a python list โ€ข how do i do it?
I Want to Remove Duplicates from a Python List โ€ข How Do I Do It?
July 15, 2025 - If that's the case, you can cast the list into a set and then back into a list to remove duplicates. But sometimes, the order matters. It certainly matters when dealing with a queue of customers.
๐ŸŒ
FavTutor
favtutor.com โ€บ blogs โ€บ remove-duplicates-from-list-python
How to Remove Duplicates from List in Python? (with code)
November 13, 2023 - It is quite quick, especially for larger lists, but the only drawback is that we will lose the order that exists in the original list. In this method, we will iterate over the whole list using a 'for' loop.
๐ŸŒ
datagy
datagy.io โ€บ home โ€บ python posts โ€บ python: remove duplicates from a list (7 ways)
Python: Remove Duplicates From a List (7 Ways) โ€ข datagy
December 19, 2022 - ... # Remove Duplicates from a Python list using a For Loop duplicated_list = [1,1,2,1,3,4,1,2,3,4] deduplicated_list = list() for item in duplicated_list: if item not in deduplicated_list: ...
Find elsewhere
๐ŸŒ
Guru99
guru99.com โ€บ home โ€บ python โ€บ how to remove duplicates from a list in python
How to Remove Duplicates from a List in Python
August 12, 2024 - You can remove duplicates from ... you the distinct elements in an order in which the key is present. You can make use of a for-loop that we will traverse the list of items to remove duplicates....
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python-ways-to-remove-duplicates-from-list
Ways to remove duplicates from list in Python - GeeksforGeeks
Let's explore other different ways to remove duplicates from list: ... To remove duplicates while keeping the original order, we can use a loop (for loop) to add only unique items to a new list.
Published ย  November 18, 2024
๐ŸŒ
Tutorial Gateway
tutorialgateway.org โ€บ python-program-to-remove-duplicates-from-list
Python Program to Remove Duplicates from List
April 7, 2025 - dupList = [1, 2, 3, 2, 4, 8, 9, ... ... This Python program allows entering the list size and items. The for loop will iterate the dupList items....
๐ŸŒ
Unstop
unstop.com โ€บ home โ€บ blog โ€บ remove duplicates from python list | 12 ways +code examples
Remove Duplicates From Python List | 12 Ways +Code Examples
February 3, 2025 - Ways to remove duplicates from Python lists are: for loop, set() function, list comprehension, dictionary (fromkeys()), in and not in operators, Counter, etc.
๐ŸŒ
DataCamp
datacamp.com โ€บ tutorial โ€บ python-how-to-remove-the-duplicates-from-a-list
Python: Remove Duplicates From A List (Five Solutions) | DataCamp
July 16, 2024 - To remove duplicates from a Python list while preserving order, create a dictionary from the list and then extract its keys as a new list: list(dict.fromkeys(my_list)).
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_howto_remove_duplicates.asp
How to remove duplicates from a Python List
Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Practice Problems Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Bootcamp Python Training ... Learn how to remove duplicates from a List in Python.
๐ŸŒ
iO Flood
ioflood.com โ€บ blog โ€บ python-remove-duplicates-from-list
3 Python Methods to Remove Duplicates From a List
June 28, 2024 - This method involves creating a new list and adding items to it only if they are not already present. The synta for this would be, if item not in list_without_duplicates: list_without_duplicates.append(item).
Top answer
1 of 16
2340

The common approach to get a unique collection of items is to use a set. Sets are unordered collections of distinct objects. To create a set from any iterable, you can simply pass it to the built-in set() function. If you later need a real list again, you can similarly pass the set to the list() function.

The following example should cover whatever you are trying to do:

>>> t = [1, 2, 3, 1, 2, 3, 5, 6, 7, 8]
>>> list(set(t))
[1, 2, 3, 5, 6, 7, 8]
>>> s = [1, 2, 3]
>>> list(set(t) - set(s))
[8, 5, 6, 7]

As you can see from the example result, the original order is not maintained. As mentioned above, sets themselves are unordered collections, so the order is lost. When converting a set back to a list, an arbitrary order is created.

Maintaining order

If order is important to you, then you will have to use a different mechanism. A very common solution for this is to rely on OrderedDict to keep the order of keys during insertion:

>>> from collections import OrderedDict
>>> list(OrderedDict.fromkeys(t))
[1, 2, 3, 5, 6, 7, 8]

Starting with Python 3.7, the built-in dictionary is guaranteed to maintain the insertion order as well, so you can also use that directly if you are on Python 3.7 or later (or CPython 3.6):

>>> list(dict.fromkeys(t))
[1, 2, 3, 5, 6, 7, 8]

Note that this may have some overhead of creating a dictionary first, and then creating a list from it. If you donโ€™t actually need to preserve the order, youโ€™re often better off using a set, especially because it gives you a lot more operations to work with. Check out this question for more details and alternative ways to preserve the order when removing duplicates.


Finally note that both the set as well as the OrderedDict/dict solutions require your items to be hashable. This usually means that they have to be immutable. If you have to deal with items that are not hashable (e.g. list objects), then you will have to use a slow approach in which you will basically have to compare every item with every other item in a nested loop.

2 of 16
492

In Python 2.7, the new way of removing duplicates from an iterable while keeping it in the original order is:

>>> from collections import OrderedDict
>>> list(OrderedDict.fromkeys('abracadabra'))
['a', 'b', 'r', 'c', 'd']

In Python 3.5, the OrderedDict has a C implementation. My timings show that this is now both the fastest and shortest of the various approaches for Python 3.5.

In Python 3.6, the regular dict became both ordered and compact. (This feature is holds for CPython and PyPy but may not present in other implementations). That gives us a new fastest way of deduping while retaining order:

>>> list(dict.fromkeys('abracadabra'))
['a', 'b', 'r', 'c', 'd']

In Python 3.7, the regular dict is guaranteed to both ordered across all implementations. So, the shortest and fastest solution is:

>>> list(dict.fromkeys('abracadabra'))
['a', 'b', 'r', 'c', 'd']
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ remove duplicates from list python
How to Remove Duplicates from List in Python? (With Examples) - Scaler Topics
February 27, 2024 - The basic approach to removing duplicates from a list in Python is to iterate through the elements of the list and store the first occurrence of an element in a temporary list while ignoring any other occurrence of that particular element. In the naive method, the basic approach is implemented ...
๐ŸŒ
Python Guides
pythonguides.com โ€บ remove-duplicates-from-a-list-in-python
How To Remove Duplicates From A List In Python?
March 19, 2025 - Check out How to Divide Each Element in a List by a Number in Python? Another way to remove duplicates is by using a for loop to iterate through the list and add unique elements to a new list in Python.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python-remove-duplicates-list
Python - Remove Duplicates from a List - GeeksforGeeks
December 5, 2024 - Dictionaries in Python 3.7+ maintain insertion order making this method efficient way to remove duplicates while preserving order. ... dict.fromkeys(a) creates a dictionary with list elements as keys automatically discarding duplicates. Convert the dictionary back to a list using list(). Using loop is useful for beginners as it follows step-by-step logic to remove duplicates.