[Tutor] Possible usage of dict comprehensions

Manprit Singh manpritsinghece at gmail.com
Mon Oct 19 19:19:51 EDT 2020


Dear sir ,

Assume I am using Python 3.7 or later, in which dictionary insertion order
is maintained. If I want to remove duplicates from a list while maintaining
the order of its elements, doing like this is a valid and a useful use case
?

>>> l = [2, 4, 5, 3, 4, 5, 2, 1]   # List whose duplicates have to be
removed
>>> list({i:None for i in l})
[2, 4, 5, 3, 1]                   # Resultant list with duplicates removed
, order preserved

Regards
Manprit Singh


More information about the Tutor mailing list