-1

Here my problem is I have a list of tuples and I need to find out who have the highest score,and corresponding name of the player.

Sample Input:

l=[
  ('ram'  ,16),
  ('sara' ,13),
  ('akhil',24),
  ('vinay',24)
]

Sample Output:

akhil 24

Explanation:

First I sorted the data based on second element in list of tuple. Among the all players "akhil" and "vinay" has same scores so among the two people player "akhil" starts first in dictionary or lexicographical order so I printed "akhil" .

Thanks.

snakecharmerb
  • 47,570
  • 11
  • 100
  • 153
Ramakrishna K
  • 43
  • 1
  • 1
  • 8
  • Does this answer your question? [How to find the maximum value in a list of tuples?](https://stackoverflow.com/questions/13145368/how-to-find-the-maximum-value-in-a-list-of-tuples) – snakecharmerb Aug 26 '23 at 13:38

2 Answers2

0

Try this.

from collections import OrderedDict
l=[('ram',16),('sara',13),('akhil',24),('vinay',24)]
a = {}
for i in l:
    a[i[0]]=i[1]
a = OrderedDict(sorted(a.items(), key=lambda t: t[0]))
print max(a.iterkeys(), key=lambda k: a[k]),a[max(a.iterkeys(), key=lambda k: a[k])]
0

l=[('ram' ,16),('sara' ,13),('akhil',24),('vinay',24)]

l1=[]

l.sort(key=lambda x:x[1])

print(l)

for i in range(1,len(l)):

if(l[i-1][1]==l[i][1]):
    l1.append(l[i][0])
    l1.append(l[i-1][0])
    z=l[i-1][1]

l1.sort()

print(l1[0],z)

Ramakrishna K
  • 43
  • 1
  • 1
  • 8