How can I remove duplicates in a list in Python
I have a list a = [1,2,3,1,4,5] and I want to remove the duplicates from the list.
I tried to do like this
a = [1,2,3,1,4,5]
b = []
for i in a:
if a.count(i) == 1:
b.append(i)
print(b)
But I am getting output as [2,3,4,5] instead of [1,2,3,4,5]. Is there any easy method to get unique values?
1 Answer
5 years ago by Divya
There is a simple method in Python to remove the duplicates.
set is data structure in python that has only non-duplicates in it. So you can simply do as below
a=[1,2,3,1,4,5]
print(list(set(a)))
the above code results [1, 2, 3, 4, 5] and it contains only non-duplicate elements.
5 years ago by Divya