+ 2
Remove duplicates from a list
In Python, how can I efficiently remove duplicates from a list while maintaining the original order of elements? I have a list of integers, and I want to remove duplicate values but keep the first occurrence of each value in the order they appear. What’s the most efficient way to achieve this in Python?
2 Réponses
+ 5
You can compare the different alternatives by benchmarking:
https://stackoverflow.com/questions/1593019/is-there-any-simple-way-to-benchmark-JUMP_LINK__&&__python__&&__JUMP_LINK-script#1593034
https://superfastpython.com/benchmark-python-code/
Some already did:
https://www.peterbe.com/plog/fastest-way-to-uniquify-a-list-in-python-3.6
I tried to use set() and then make sure the output is ordered correctly. Depending on the Python implementation, the order is maintained in the set – but don't rely on it.
https://sololearn.com/compiler-playground/crWeMwdWluU0/?ref=app
+ 3
Melkon ,
from my point of view not only the *performance of execution speed* should be measured, but also the *memory consumption*.
some online platforms like leetcode are measuring these 2 parameters to judge as an additional check if your code can pass the test cases or not.