jorge lomas
jorge lomas

Reputation: 37

How to know the max of this list of tuples? (Python)

I okay so i need the max of this list:

variable=[[(0, 0), (0, 0)], [(0, 0), (0, 0)], [(0, 0), (0, 0)], [(13, '♠'), (13, '♣'), (3, '♠'), (3, '♥')], [(11, '♥'), (13, '♠'), (11, '♠'), (13, '♣')], [(11, '♥'), (13, '♠'), (11, '♣'), (13, '♣')]]

I´m using this line of code to do it but it doesn´t really work

print(max(variable,key=lambda x:x[1]))

The print says [(13, '♠'), (13, '♣'), (3, '♠'), (3, '♥')] but the one i need are one of the other two ones and i dont know what to do. Thanks in advance and sorry if i messed something up,this is my first time using stack overflow for asking a question. Im using python btw

Upvotes: 0

Views: 29

Answers (1)

Rakesh
Rakesh

Reputation: 82765

I believe you need sum of element 0 in your sub-list.

Ex:

variable=[[(0, 0), (0, 0)], [(0, 0), (0, 0)], [(0, 0), (0, 0)], [(13, '♠'), (13, '♣'), (3, '♠'), (3, '♥')], [(11, '♥'), (13, '♠'), (11, '♠'), (13, '♣')], [(11, '♥'), (13, '♠'), (11, '♣'), (13, '♣')]]
print(max(variable, key=lambda x: sum(i for i,_ in x)))
# --> [(11, '♥'), (13, '♠'), (11, '♠'), (13, '♣')]

Upvotes: 1

Related Questions