0

List filtering. HELP

I have a list with a same elements, for example: list = [3,7,8,7,7,3,6,3] How to clear this list from duplicated elements? result must be: list = [3,7,8,6]

27th Feb 2017, 8:04 PM
XENONIUM
XENONIUM - avatar
3 Answers
+ 11
First of all, do not use 'list' as a variable name, it's a reserved word :) If you have a variable: list1 = [3,7,8,7,7,3,6,3] you might want to do a type-casting trick: list2 = list(set(list1)) That will return only unique elements (as a set) and convert it to a list. print(list2) >>> [8, 3, 6, 7]
27th Feb 2017, 8:35 PM
Kuba SiekierzyƄski
Kuba SiekierzyƄski - avatar
+ 3
You can write for cycle inside of for cycle, and iterate over all numbers and delete the same ones. But the best way here is just to convert list to set data type. For example: List1=[1,1,2,2,2,3,567,68] List1=list(set(List1)) # result will be [1,2,3,567,68]
27th Feb 2017, 8:32 PM
Leshark
Leshark - avatar
+ 1
THANK YOU ALL!
27th Feb 2017, 8:36 PM
XENONIUM
XENONIUM - avatar