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]
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]
+ 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]
+ 1
THANK YOU ALL!