There are lots of answers already showing how to implement this task correctly, but none discuss your actual code.
Deconstructing your code
Hopefully this will help understand why your code did not work. Here's your code, with comments added:
def func(list1):
for i in range(len(list1)):
list2 = [x for x in list1 if x != list1[i]]
Here you construct a list, list2
, that does not contain list1[i]
.
if list1[i] in list2:
This if
statement is always going to be false, by construction of list2
.
list1.remove(i)
Even if you fix the logic of the if
statement, list1.remove(i)
would cause trouble: trying to remove elements of a list while iterating over it invalidates the indices for the rest of the iteration. It's best to avoid doing this and build the results into a new list instead.
Furthermore, list1.remove(i)
is looking for value i
in list1
and will remove it, wherever it first finds it. It's not removing the i'th element, as this code seems to expect.
return list1
And this return statement was probably not meant to be indented this deep, it should probablement have been at the same level as the for
statement at the top.
Working code closer to your logic
While I don't think this is the best solution -- the other answers provide better ones -- here is a solution inspired from your code, which works.
def func(list1):
list2 = [] # We'll append elements here
for item in list1: # more pythonic way to loop over a list
if item not in list2:
list2.append(item)
return list2
My preferred solution
OK, so now I'm duplicating information in other answers, but with Python 3.7(*) or more recent, where dict
is guaranteed to keep the order of the elements inserted into it, I'd use @Pedro Maia's solution:
def func(list1):
return list(dict.fromkeys(list1))
In fact, I used that technique in my code base recently.
In older versions of Python, dicts are not ordered. However, there has been the OrderedDict
class in the collections
library since Python 2.7, so this code would work with any version of Python >= 2.7:
from collections import OrderedDict
def func(list1):
return list(OrderedDict.fromkeys(list1))
If you're not familiar with them, have a look at the collections
library and the itertools
library, they provide immensely useful helpers for all sorts of problems.
(*) According to the manual for collections.OrderedDict
, Python dict
s have only been guaranteed to be ordered since Python 3.7, even though they started actually being ordered in Python 3.6, at least for the CPython implementation. See also: Are dictionaries ordered in Python 3.6+?