When using set()
to remove duplicates values from a list the values order of insertion is not preserve:
mylist = ['b', 'b', 'a', 'd', 'd', 'c'] results = list(set(mylist)) print(results) # output >>> ['a', 'd', 'b', 'c']
To preserve the values insertion order when removing duplicated values from a list, I found away around set()
by using a dictionary
as fallow:
mylist = ['b', 'b', 'a', 'd', 'd', 'c'] results = list({value:"" for value in mylist}) print(results) >>> ['b', 'a', 'd', 'c']
Does a better method exist, than one that I used above, to duplicates values from a list when using a dictionary instead of set(), to preserve the the values insertion order?