Reputation: 1067
In Python I have some data that looks like this:
A = [
(-9, [(2, 3), (5, 2), (3,1)]),
(-8, [(3, 4), (5, 6), (7, 6)]),
(-8, [(0, 0), (5, 0), (1, 6)]),
(-9, [(2, 3), (4, 2), (4, 5)]),
]
What is the most pythonic way to extract the element or elements that have the maximum value in the first entry? Changing the data structure is not an option.
In this example, the goal is to return the two middle tuples.
Cheers!
Upvotes: 2
Views: 2472
Reputation: 838696
Try a list comprehension:
max_value = max(x[0] for x in A)
print [x for x in A if x[0] == max_value]
See it working online: ideone
Upvotes: 8