4 Comments
User's avatar
Timothy Grant's avatar

This was excellent thank you. I have often used sets to dedupe, but never used a dictionary to.

Great tip!

Expand full comment
Stephen Gruppetta's avatar

Sets are the ideal route if you don’t care about order (and all elements are hashable)

Expand full comment
B Sha's avatar
9hEdited

Hi Stephen, we can create a new empty list and iterate on first list which has non-unique values and check if an item is not in second list - if so append the item to second list. This approach also seems to maintain the order of the first list and creates unique values.

Expand full comment
Stephen Gruppetta's avatar

Indeed, that works too. But going through dict.fromkeys() will be faster (I believe).

Expand full comment