We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • HackerRank Home

    HackerRank

  • |
  • Prepare
  • Certify
  • Compete
  • Hiring developers?
  1. Prepare
  2. Python
  3. Basic Data Types
  4. Nested Lists
  5. Discussions

Nested Lists

Problem
Submissions
Leaderboard
Discussions
Editorial
Tutorial

    You are viewing a single comment's thread. Return to all comments →

  • chaz4
    2 months ago+ 1 comment

    With comments for clarity

    grades = []
    answer = []
    #add a line in the given loop to add 
    #names and scores to a NESTED LIST
    for _ in range(int(input())):
        name = input()
        score = float(input())
        grades.append([name, score])
    
    #sort the nested list by scores,
    #then by name (in case of a tie)
    grades.sort(key = lambda l: (l[1], l[0]))
    
    #make a SET of just the scores to
    #remove repeat values (so we will
    #know which score is 2nd lowest)
    gradeset = set(grades[i][1] for i in range(len(grades)))
    
    #convert the above set into a list
    #so we can iterate through it
    uniquelist = list(gradeset)
    
    #iterate through the nested list
    #if the grade in the list is equal
    #to the second score in the unique
    #list, add that person's name to
    #the final answer list
    for j in range(len(grades)):
        if grades[j][1] == uniquelist[1]:
            answer.append(grades[j][0])
    
    #print the names you got from the
    #above step, each one on a new line
    for x in answer:
        print(x)
    
    0|
    Permalink
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy