De-duplicate with sets & dict.fromkeys
Saw this tip from Trey Hunner’s newsletter.
Do you need to de-duplicate items in a list?
| |
If you don’t care about maintaining the order of your items, you could use the built-in set constructor:
| |
If you do care about maintaining the order of your items, you could use the dict class’s fromkeys method along with the built-in list constructor:
| |
Note that those two techniques only work for numbers, strings, or other hashable objects.