I want to make a function that takes a list and returns the same list but without the duplicate elements
def func(list1):
for i in range(len(list1)):
list2 = [x for x in list1 if x != list1[i]]
if list1[i] in list2:
list1.remove(i)
return list1
mylist = [1,2,3,4]
func(mylist)
CodePudding user response:
You can just do this:
def remove_duplicates(l):
return list(set(l))
Now remove_duplicates([1,1,2,3,3,4,5,6,6,7])
will return [1, 2, 3, 4, 5, 6, 7]
.
CodePudding user response:
Let's assume this input l = [1,2,3,2,7,3,4,4,7,1]
removing the duplicates without keeping order
use a python set:
list(set(l))
output: [1, 2, 3, 4, 7]
keeping order
use a dictionary (or collections.Counter
)
from collections import Counter
list(Counter(l))
output: [1, 2, 3, 7, 4]
CodePudding user response:
Use a set:
mylist = [1,2,3,1,3,2,4]
mylist = set(mylist)
print(mylist)
Output:
{1, 2, 3, 4}
To keep the order you can do:
mylist = list(dict.fromkeys(mylist))
which doesn't require any library
CodePudding user response:
In python we can use the set to remove the duplicate elements:
def removeDuplicates(l):
return list(set(l))
mylist = [1, 2, 3, 4, 2, 3, 4, 2, 3, 4]
newList = removeDuplicates(mylist)
print(newList)
CodePudding user response:
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.6 or more recent, where dict
keeps the order of the elements inserted into it, I'd use @Pedro Maia's solution:
def func(list1):
return list(dict.fromkeys(list1))
In older versions of Python, dicts are not ordered, so you'd have to pick another solution.