0

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?

Alex Ricciardi
  • 181
  • 1
  • 9
  • 1
    `list(dict.from_keys(mylist))` – Dani Mesejo Dec 11 '20 at 20:48
  • 2
    @DaniMesejo: `dict.fromkeys`, not `dictionary.from_keys`, but close. – ShadowRanger Dec 11 '20 at 20:50
  • 2
    @Alex: Yeah, your solution is a good one, and it's what `dict.fromkeys` does more efficiently than a hand-rolled `dict` comprehension (it sets all the values to `None` by default, not the empty string, but given you don't use the values, it hardly matters) by doing the work entirely at the C layer, where a `dict` comprehension still involves the bytecode interpreter (even if it does use specialized, optimized bytecodes to run faster than building a `dict` with regular loops and assignment). For longish inputs, `dict.fromkeys` shaves the runtime by about a quarter (and it's shorter to type). – ShadowRanger Dec 11 '20 at 20:53

0 Answers0