Spyros
Spyros

Reputation: 259

Indexing Elements in a list of tuples

I have the following list, created by using the zip function on two separate lists of numbers:

[(20, 6),
(21, 4),
(22, 4),
(23, 2),
(24, 8),
(25, 3),
(26, 4),
(27, 4),
(28, 6),
(29, 2),
(30, 8)]

I would like to know if there is a way to iterate through this list and receive the number on the LHS that corresponds to the maximum value on the RHS, i.e. in this case I would like to get 24 and 30, which both have a value of 8 (the max value of the RHS). I have tried:

## get max of RHS 
max_1 = max([i[1] for i in data])
## for max of LHR, index this location
## on RHS column 
number = [i[1] for i in data].index(max_1)

but it doesn't work.

Upvotes: 2

Views: 103

Answers (1)

After:

max_1 = max([i[1] for i in data])

Try:

>>> number = [i[0] for i in data if i[1]==max_1]
>>> number
[24, 30]

Upvotes: 9

Related Questions