0

I'm trying to write a function that takes in

  1. a list of tuples with partygoers names and their priority and
  2. the max number of people that can get it. I'm currently trying to put tuples in ascending order based on one of its elements.

I have a list or tuples:

 alist =[('Carlos', 1), ('Gretchen', 8), ('Abby', 6),('Sam', 4)]

so far Ive made a list of all of the numbers in order with this code:

 def party_time(alist,maxnum):
     newlist = []
     finallist=[]
     while len(finallist) < maxnum:
        for tup in alist:
            rank = tup[1]
            newlist.append(rank)
        newlist.sort()

 newlist = [1,4,6,8]

Is there a way to use a for loop to go through newlist and index the tuple by using the number in newlist? Or will it just index where the number is in the tuple?

Alex Fung
  • 1,996
  • 13
  • 21
Malaikatu Kargbo
  • 313
  • 2
  • 12

1 Answers1

0

This should do it:

def party_time(alist, maxnum):
    sorted_list = sorted(alist, key=lambda p: p[1], reverse=True)
    return sorted_list[:maxnum]
Lucas
  • 4,067
  • 1
  • 20
  • 30