0

How to remove duplicate number

21st Oct 2018, 9:04 AM
Goodboy08
Goodboy08 - avatar
2 Respostas
+ 3
If you mean from a list, just transform it into a set: k=[1, 1,1,1,15] s=set(k)
21st Oct 2018, 9:07 AM
fra
fra - avatar
+ 2
If the duplicates are in a list, and you want to remove duplicates while keeping the order of the numbers, you can use a for loop to insert the number to another list. While inserting, check whether a number already exists in the destination list. dups = [1,9,2,3,8,3,0,7,4,1,6,5,0,5] unique = [] for n in dups: if not n in unique: unique.append(n) print(dups) print(unique) This requires more lines written, but the numbers will not be sorted; as it will if we make a set from the list. Hth, cmiiw
21st Oct 2018, 9:53 AM
Ipang