Nested Lists in Python - Hacker Rank Solution


Nested Lists in Python - Hacker Rank Solution


Problem


Tutorial :

Let's implement a nested list! A nested list is a list that contains another list (i.e.: a list of lists). For example:
nested_list = [['blue', 'green'], ['red', 'black'], ['blue', 'white']]
print len(nested_list)
# prints 3
print nested_list[1]
# prints ['red', 'black']
print nested_list[1][0]
# prints red

To go through every element in a list, use a nested for loop.
Given the names and grades for each student in a Physics class of N students, store them in a nested list and print the name(s) of any student(s) having the second lowest grade.
Note: If there are multiple students with the same grade, order their names alphabetically and print each name on a new line.



Input Format :

The first line contains an integer, N, the number of students.
The 2N subsequent lines describe each student over 2 lines; the first line contains a student's name, and the second line contains their grade.

Constraints :

  • 2 <= N <=5
  • There will always be one or more students having the second lowest grade.

Output Format :

Print the name(s) of any student(s) having the second lowest grade in Physics; if there are multiple students, order their names alphabetically and print each one on a new line.



Sample Input :

5
Harry
37.21
Berry
37.21
Tina
37.2
Akriti
41
Harsh
39

Sample Output :

Berry
Harry

Explanation :

There are 5 students in this class whose names and grades are assembled to build the following list: python students = [['Harry', 37.21], ['Berry', 37.21], ['Tina', 37.2], ['Akriti', 41], ['Harsh', 39]]
The lowest grade of 37.2 belongs to Tina. The second lowest grade of 37.21 belongs to both Harry and Berry, so we order their names alphabetically and print each name on a new line.



Solution :


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
# Nested Lists in Python - Hacker Rank Solution  
# python2 solution
score_list = []; # do not forget to declare a list for _ in range(int(raw_input())): name = raw_input() score = float(raw_input())
    # Nested Lists in Python - Hacker Rank Solution START score_list.append([name, score]) second_highest = sorted(set([score for name, score in score_list]))[1] print('\n'.join(sorted([name for name, score in score_list if score == second_highest])))
# Nested Lists in Python - Hacker Rank Solution END





Disclaimer :-
the above hole problem statement is given by hackerrank.com but the solution is generated by the codeworld19 authority if any of the query regarding this post or website fill the following contact form thank you.

Next Post Previous Post
30 Comments
  • Imran khan
    Imran khan Wednesday, December 23, 2020

    it was showing me some error

    • CodeWorld19
      CodeWorld19 Thursday, December 24, 2020

      this problem solution are in python2. you have need to select python2 editor on hackerrank site then it will be correct. i think you have try to run it on python3 that's why they have show an error.
      sorry for late replay......

      Thank You

    • Unknown
      Unknown Thursday, July 01, 2021

      Use \n ,not /n .

  • rahul kumar mahato
    rahul kumar mahato Wednesday, April 14, 2021

    please solve this question in python 3
    and also try to solve all the question in python 3

    • Krishnendu Roy
      Krishnendu Roy Thursday, April 15, 2021

      score_list = []
      for _ in range(int(input())):
      name = input()
      score = float(input())

      score_list.append([name, score])
      second_highest = sorted(set([score for name, score in score_list]))[1]
      print('\n'.join(sorted([name for name, score in score_list if score == second_highest])))



      all same only use input. raw_input for python 2 and input for python3

  • loki
    loki Friday, April 16, 2021

    if __name__ == '__main__':
    records=[]
    scores=[]
    final=[]
    n=int(input())
    for _ in range(n):
    name = input()
    score = float(input())
    records.append([name, score])
    scores.append(score)
    scores.sort()
    for i in range(len(scores)):
    if scores[i]<scores[i+1]:
    x=scores[i+1]
    break
    for i in range(n):
    if records[i][1]==x:
    final.append(records[i][0])
    final.sort()
    for j in range(len(final)):
    print(final[j])


    my code is kinda big but can be understood by newbies

    • Revanth Kodukula
      Revanth Kodukula Monday, May 17, 2021

      Yes im totally confused with the small code they have given.

    • Unknown
      Unknown Friday, March 18, 2022

      that's right

    • Anonymous
      Anonymous Tuesday, May 03, 2022

      Your code doesnt fulfill two test cases

  • Unknown
    Unknown Saturday, June 05, 2021

    if __name__ == '__main__':
    res=[]
    score_1=[]
    name_1=[]
    final=[]
    n=int(input())
    for i in range(n):
    name = input()
    score = float(input())
    res.append([name,score])
    name_1.append(name)
    score_1.append(score)
    score_1.sort()

    temp=score_1[1]
    for i in res:
    if i[1]==temp:
    final.append(i[0])
    final.sort()
    for i in final:
    print(i)

    • Unknown
      Unknown Wednesday, June 09, 2021

      Score_1[] ko sort karne se pahle
      score_1=list(set(score_1))
      Karo jisse duplicate remove ho jayenge to hame temp sahi milega sakhi condition me

  • Unknown
    Unknown Monday, September 06, 2021

    if __name__ == '__main__':
    score_list=[]
    sc=[]
    for _ in range(int(input())):
    name = input()
    score = float(input())
    score_list.append([name,score])
    sc.append(score)
    sc=list(set(sc))
    sc.sort()
    score_list.sort()
    sh = sc[1]
    for name,score in score_list:
    if score==sh:
    print(name)

    • Axalotle
      Axalotle Thursday, September 16, 2021

      your code is very clearly understandable for a newbie
      Thank you very much

    • Unknown
      Unknown Tuesday, October 05, 2021

      what is __name__ = '__main__'

    • Anonymous
      Anonymous Tuesday, May 03, 2022

      Entry point for the program

  • CodeWorld19
    CodeWorld19 Saturday, September 18, 2021

    Thank You guys for giving alternate code in comment section.

  • knockbell Works
    knockbell Works Friday, October 08, 2021

    This comment has been removed by the author.

  • Unknown
    Unknown Friday, December 03, 2021

    if __name__ == '__main__':
    scoreList = []
    listx = []
    for _ in range(int(input())):
    name = input()
    score = float(input())
    scoreList.append(score)
    listx.append([name,score])


    scoreList= list(set(scoreList))
    scoreList.sort()

    secondLast=scoreList[1]
    listx.sort()

    for x in listx:
    if (x[1] == secondLast ):
    print(x[0])

  • VECTOR CODER
    VECTOR CODER Sunday, December 19, 2021

    Check this:
    FOR Python 3:

    if __name__ == '__main__':
    L = []
    scoreL = []
    for _ in range(int(input())):
    name = input()
    score = float(input())
    scoreL.append(score)
    subL = [score,name]
    L.append(subL)
    scoreL.sort()
    L.sort()
    for i in range(len(scoreL)):
    if scoreL[i] > scoreL[0]:
    sg = scoreL[i]
    break
    for p in L:
    if sg in p:
    print(p[1])

  • Kami
    Kami Tuesday, December 21, 2021

    What does "score for name" and "name for name" mean

    • Kami
      Kami Tuesday, December 21, 2021

      records = []
      scores = []
      if __name__ == '__main__':
      for _ in range(int(input())):
      name = input()
      score = float(input())
      records.append([name, score])
      scores.append(score)

      second_highest = sorted(set(scores))[1]

      names = []
      for record in records:
      if second_highest in record:
      names.append(record[0])
      names.sort()
      print('\n'.join (names))

    • Ujjwal Sharma
      Ujjwal Sharma Tuesday, January 11, 2022

      What does this " print('\n'.join (names)) " means ?

    • Unknown
      Unknown Friday, January 14, 2022

      Its means join \n in your name .The output is : #for \n we get the in the new line
      name

  • bhanu
    bhanu Friday, December 24, 2021

    Thank you very much... Good Article!!
    Python Online Course
    Python Online Training in India

  • Unknown
    Unknown Friday, January 14, 2022

    if __name__ == '__main__':
    res=[]
    score_1=[]
    name_1=[]
    final=[]
    for _ in range(int(raw_input())):
    name = raw_input()
    score = float(raw_input())
    res.append([name,score])
    name_1.append(name)
    score_1.append(score)
    score_1.sort()
    c=[]
    for i in score_1:
    if i>score_1[0]:
    c.append(i)
    c.sort()
    temp=c[0]
    for i in res:
    if i[1]==temp:
    final.append(i[0])
    final.sort()
    for i in final:
    print(i)

  • Unknown
    Unknown Saturday, January 15, 2022

    my_dict = {}
    for _ in range(int(input())):
    name = input()
    score = float(input())
    my_dict[name] = score

    for key, value in my_dict.items():
    if sorted(my_dict.values())[2] == value:
    print(key)

  • Mayank Kaushik
    Mayank Kaushik Tuesday, January 18, 2022

    n=int(input())
    a=[]
    d=[]
    e=[]
    for i in range(0,n):
    b=[]
    name=input()
    b.append(name)
    score=(float(input()))
    b.append(score)
    a.append(b)
    def Sort(a):
    a.sort(key =lambda x:x[0])
    a.sort(key=lambda x:x[1])

    return a
    b=Sort(a)
    for i in range(n):
    if b[i][1]>b[0][1]:
    d.append(b[i])
    break
    for i in range(0,n):
    if d[0][1]==a[i][1]:
    e.append(a[i][0])
    for i in range(len(e)):
    print(e[i])

  • Anonymous
    Anonymous Thursday, May 26, 2022

    In the line of sorted_score = sorted(set([score for name, score in score_list]))[1]...
    What is the use of (1) in the statement

  • Hasibur Rahman
    Hasibur Rahman Sunday, July 24, 2022

    if __name__ == '__main__':
    list1=[]
    for _ in range(int(input())):
    name = input()
    score = float(input())
    list1.append([name,score])
    x=list1[1][1]
    list2=[]
    n=len(list1)
    for j in range(n):
    if x==list1[j][1] :
    list2.append(list1[j][0])
    list2.sort(key=lambda x:x[0])
    for i in list2:
    print(i)

  • Anonymous
    Anonymous Thursday, July 28, 2022

    hope this help:
    if __name__ == '__main__':
    data=[]
    for _ in range(int(input())):
    name = input()
    score = float(input())
    data.append([name,score])
    data.sort(key = lambda x :x [1]) #Sort the value from the largest to smallest
    min_value = min(data, key = lambda item : item[1])
    second_min_value= 0
    for k in range(len(data)):
    if data[k][1] > min_value[1]:
    second_min_value = data[k][1]
    break
    #rearrage the alphabet
    all_value=[]
    for j in range(len(data)):
    if second_min_value == data[j][1] :
    all_value.append(data[j][0])
    all_value.sort(key=lambda x:x[0])
    for i in all_value:
    print(i)

Add Comment
comment url