[Tutor] Possible usage of dict comprehensions
Mark Lawrence
breamoreboy at gmail.com
Mon Oct 19 20:34:48 EDT 2020
On 20/10/2020 00:42, Alan Gauld via Tutor wrote:
> On 20/10/2020 00:19, Manprit Singh wrote:
>> 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
>
> Well you have demonstrated that it works so it must be valid.
> How useful it is depends on whether you need to perform that operation.
> Assuming you do then yes its useful.
>
> It's probably fairly efficient too but I haven't timed it.
>
Plenty of background here
https://stackoverflow.com/questions/480214/how-do-you-remove-duplicates-from-a-list-whilst-preserving-order
--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.
Mark Lawrence
More information about the Tutor
mailing list