Delete multiple same elements from Python List
Sep 3, 2018 · 1 min read
Let we have a list contains different elements. Among these elements, some elements repeat several times, for instance we have three 1s and two 3s in our list like
l1 = [1, 2, 3, 4, 1, 1, 5, 6, 3, 7]
We will delete the elements that occurs multiple times. Let’s do it:
l1 = [1, 2, 3, 4, 1, 1, 5, 6, 3, 7]
l2 = [] #empty list
l3 = [] # empty list
for i in l1:
….if i not in l2:
……..l2.append(i)
….else:
……..l3.append(i)
for j in l2[:]:
….if j in l3:
……..l2.remove(i)
print l2
Output: [2, 3, 4, 5, 6, 7]
