0

Remove duplicate items from list

how to Remove duplicate items from list and leave only one items from every duplicated: ex : a = [1,1,1,2,2,2,3,3,3] result = [1,2,3]

20th Mar 2018, 12:23 PM
Mohamed Rashidey Hasan
Mohamed Rashidey Hasan - avatar
5 Respuestas
+ 6
#You can also do like this a = [1,6,1,4,6,9,1,6,9,1] uniq = [] for i in a: if i not in uniq: uniq.append(i) print(uniq)
20th Mar 2018, 1:54 PM
Ferhat Sevim
Ferhat Sevim - avatar
+ 5
set sorts the list and can only hold a value once so duplicates are removed. If you wish to maintain the original order, this can be used. https://code.sololearn.com/cdXh680icSEt
20th Mar 2018, 1:04 PM
John Wells
John Wells - avatar
+ 2
# Convert to a set and back into a list. num= set(a) result = list(num) print(result)
20th Mar 2018, 12:48 PM
Manorama
Manorama - avatar
+ 2
sets have only unique elements
20th Mar 2018, 1:08 PM
Manorama
Manorama - avatar
0
why when convert to set duplicated remove ?
20th Mar 2018, 12:59 PM
Mohamed Rashidey Hasan
Mohamed Rashidey Hasan - avatar