How do I make unique list of tuples by their first values in the most Pythonic way?
Example:
list_of_tuples = [('a', 1), ('a', 2), ('b', 3)]
# Apply here magical Pythonic one liner.
print(list_of_tuples)
[('a', 1), ('b', 3)]
How do I make unique list of tuples by their first values in the most Pythonic way?
Example:
list_of_tuples = [('a', 1), ('a', 2), ('b', 3)]
# Apply here magical Pythonic one liner.
print(list_of_tuples)
[('a', 1), ('b', 3)]
Using itertools.groupby
:
[next(g) for _, g in groupby(tups, key=lambda x:x[0])]
Output:
[('a', 1), ('b', 3)]
If the original list needs to sorted:
tups = [('a', 1), ('a', 2), ('b', 3), ('a', 3)]
f = lambda x:x[0]
[next(g) for _, g in groupby(sorted(tups, key=f), key=f)]
Output:
[('a', 1), ('b', 3)]
Here's a pandas one liner:
import pandas as pd
pd.DataFrame([('a', 1), ('a', 2), ('b', 3)]).groupby(0).min().to_dict()[1]
# {'a': 1, 'b': 3}
The most pythonic way would be to solve that with a set comprehension and a map to get the original values back. something like
[*map(lambda x: next(t for t in list_of_tuples if t[0] == x), {t[0] for t in list_of_tuples})]
But don't understand me wrong, i like chris answer, it is more efficient. Mine uses a double for loop, so it technically works, but can be way improved in efficiency.
The constructor for dicts takes lists of tuples. The first element in the pair would need to be hashable for this to work:
list(dict(list_of_tuples).items())
Update: Not only is this solution simpler than @Chris', but it is almost an order of magnitude faster. For runs of situations where the first value in the tuples is almost always the same, more often than not the same, and where the first values are approximately evenly distributed, I get the following timeit
s:
almost all same first values: Scott 2.229072586997063, Chris 11.897218765003345
many same first values: Scott 2.077339955998468, Chris 10.501757369002007
even distributed first values: Scott 2.197656606000237, Chris 9.972954122000374
© 2022 - 2024 — McMap. All rights reserved.