Unique List of Tuples By First Value of Tuples [duplicate]
Asked Answered
D

4

6

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)]
Delores answered 13/12, 2019 at 7:12 Comment(0)
W
5

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)]
Wither answered 13/12, 2019 at 7:16 Comment(0)
C
1

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}
Crus answered 13/12, 2019 at 7:41 Comment(0)
H
0

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.

Hypersthene answered 13/12, 2019 at 7:43 Comment(0)
S
-1

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 timeits:

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

Sanderling answered 13/12, 2019 at 7:20 Comment(4)
Casting to list removes second values of tuples.Delores
@Delores easy fix. Sometimes I post answers from my phone, and they're not "tested" or even "correct". :DSanderling
Making a dict will yield tuples with the last inserted items, which is not what OP wantsWither
@Chris, the OP doesn't specify that.Sanderling

© 2022 - 2024 — McMap. All rights reserved.