how can I maintain sequence of my list using set?
Solution 1
If you are not concerned with efficiency, this is O(n*m)
>>> sorted(set(l1), key=l1.index)
['a', 2, 3, 0, 9.0, 6, 'b']
Using an intermediate dict is more complicated, but is O(n+m*logm)
where n is the number of elements in l1 and m is the number of unique elements in l1
>>> l1 = ['a',2,3,0,9.0,0,2,6,'b','a']
>>> d1=dict((k,v) for v,k in enumerate(reversed(l1)))
>>> sorted(d1, key=d1.get, reverse=True)
['a', 2, 3, 0, 9.0, 6, 'b']
In Python3.1 you have OrderedDict so it's very easy
>>> l1 = ['a',2,3,0,9.0,0,2,6,'b','a']
>>> list(OrderedDict.fromkeys(l1))
['a', 2, 3, 0, 9.0, 6, 'b']
Solution 2
You can solve it by defining a function like this:
def dedupe(items):
seen = set()
for item in items:
if item not in seen:
yield item
seen.add(item)
To use it:
>>> l1 = ['a',2,3,0,9.0,0,2,6,'b','a']
>>> l2 = list(dedupe(l1))
>>> l2
['a', 2, 3, 0, 9.0, 6, 'b']
shahjapan
Passion: Computer Science, Programming, R & D, Troubleshooting Programming: Python, C#, Shell scripting Frontend Technologies: Bootstrap, Javascript, JQuery, SCSS Cloud Technologies: GCP, AWS Virtualisation: Docker, Docker-Compose, k8s RDBMS: MySQL, PostgreSQL, SQLite Operating Systems: Linux, Windows, OSX Testing: Unit Tests, pytest, Jenkins, selenium Hobbies: Playing Chess, Swimming & roaming -- Japan Shah
Updated on July 10, 2022Comments
-
shahjapan almost 2 years
In [1]: l1 = ['a',2,3,0,9.0,0,2,6,'b','a'] In [2]: l2 = list(set(l1)) In [3]: l2 Out[3]: ['a', 0, 2, 3, 6, 9.0, 'b']
Here you can see the the list l2 is falling with different sequence then the original l1, I need to remove the duplicate elements from my list without changing the sequence/order of the list elements....