• + 63 comments

    Easy pythonic code that anyone can understand

    l = []
    second_lowest_names = []
    scores = set()
    
    for _ in range(int(input())):
        name = input()
        score = float(input())
        l.append([name, score])
        scores.add(score)
            
    second_lowest = sorted(scores)[1]
    
    for name, score in l:
        if score == second_lowest:
            second_lowest_names.append(name)
    
    for name in sorted(second_lowest_names):
        print(name, end='\n')
    
    • + 1 comment

      Great use of the set function to eliminate repeating scores! I didn't know about sets and learned about its usefulness from your solution thanks!!

      • + 1 comment

        Thanks!

    • + 1 comment

      if you appiled fo 8 elment, this code will fail bro. this code only use for 5 input

      • + 1 comment

        How? can you explain it.

    • + 0 comments

      Python newbie here - finally something I can understand!! I struggle to follow the logic of other solutions proposed here... sobs Someone mentioned below that this code fails when you apply 8 element - can anyone explain what this mean please?

    • + 1 comment

      scores.add(score) can you please explain this statement

      • + 0 comments

        It's basically to add the score of each student in scores = set(), but the set will store only unique values

    • + 0 comments

      Thanks

    • + 0 comments

      This is too good, really. Thank you!

    • + 0 comments

      awesome

    • + 0 comments

      can you explain line 9 !! what is happening there?

    • + 0 comments

      second_lowest = sorted(scores)[1]

      How does your code resolve the case when multiple students share the lowest score? ex. Suppose 3 people receive grades of 0.

      nvm: .add instead of .append

    • + 0 comments

      Thanks Dude

    • + 0 comments

      Hi there, I used the same code, I only replaced

      second_lowest = sorted(scores)[1]

      with

      sorted_scores=sorted(scores) second_lowest=sorted_scores[1]

      It works perfectly on spyder but here on hackerrank it file all the tests, any idea?

    • + 0 comments

      what if the lowest grade is for two students then the second lowest grade will be in the index 2

    • + 1 comment

      since you use sorted scores[1] this may not work in cases where there are 2 instances of the lowest score, other than that -- very readable code!

      • + 1 comment

        I actually made the same comment a couple of weeks ago, but I realized that since scores is a set, sorted(scores)[1] will return the second lowest score.

        • + 0 comments

          Great catch! Thanks for the clarification

    • + 0 comments

      Hey, man! Thanks a bunch for this simple, yet amazing code! I changed the lower half to make it more compact. :)

      second_lowest_students = sorted([name for (name, score) in list if score == second_lowest_score])
      
      for x in second_lowest_students:
          print(x)  
      
    • + 0 comments

      second = sorted(scores)[-2]

      for name, score in marksheet: if score == second: print (name)

    • + 1 comment

      This will not work for N=2 and both marks being entered same.

      • + 0 comments

        Have a look at the constraints! N >= 2.

    • + 0 comments

      Thanks! This was helpful.

    • + 0 comments

      Thank you so much!!

    • + 0 comments

      We can get rid of last two lines by running for loop on sorted list

      for name, score in sorted(l): if score == second_lowest: print(name)

    • + 0 comments

      second_lowest => re-compute this if student names have max_score probably appearence more than one time

    • + 0 comments

      Thank you ! Very clearly.

    • + 0 comments

      thank you very much for this lucid explanation

    • + 0 comments

      thanks you

    • + 0 comments

      superb man

    • + 0 comments

      brilliant! thanks so much ... even 3 years later.

    • + 0 comments

      I find this so clear and comprehensible, thank you. I still can't fully wrap my head around list comprehension.

    • + 0 comments

      nice bro..

    • + 0 comments

      @prasang_singhal clean and easy to follow. Thank you.

    • + 0 comments

      you cannot use add with float object

    • + 0 comments

      ur code is quite easy, thnx a more sorted way...

      n = int(input())
      arr = [[input(),float(input())] for _ in range(n)]
      marks=[arr[i][1] for i in range(n)]
      second_lowest=sorted(set(marks))[1]
      names=[arr[i][0] for i in range(n) if arr[i][1]==second_lowest]
      print('\n'.join(i for i in sorted(names)))
      
    • + 0 comments

      thank you sir

    • + 0 comments

      thank you so much the code is too easy to understand for begineers

    • + 0 comments

      Pardon my knowledge of sets, but i thought they were "unordered collection with no duplicate elements" and that you could not sort them. So how can you use the sorted(scores)[1] function?

    • + 0 comments

      What happens if you have 2 or more lowest scores? this wont work then

    • + 0 comments

      well, this was really easy to understand for rookie as myself

    • + 0 comments

      This is working bro!!!!!!!!!!

    • + 0 comments

      Thanks for the comment. For me as a begginer (started learning 3weeks ago) its the most understanable code!

    • + 0 comments

      This code is beautiful for beginners like me... explains a lot of things... thanks a ton :)

    • + 0 comments

      why did you use sorted(scores)[1], why not [-2] We need 2nd highest right? then if we sort then [-2] will be the second highest could you please explain it to me brother 🤔

    • + 0 comments

      why have you used scores.add(score) what is the need of that line

    • + 0 comments

      Thank you!

    • + 0 comments

      nice solution

    • + 0 comments

      the world needs more people like you

    • + 0 comments

      Now this is how one writes a simple code.

    • + 0 comments

      this is very elegant, thanks!

    • + 0 comments

      very helpfull

    • + 0 comments

      Of the top solutions, this is the best IMO. I don't know why we want to encourage code that is not readable. In a real world development environment, you are supposed to write code that your peers can understand right away. Not spend 20 minutes deciphering your 1 liner. Thank you sir.

    • + 0 comments

      thanks

    • + 0 comments

      Found this really helpful

    • + 0 comments

      You were so right when you said "anyone can understand" . Actually it is. Thank You.

    • + 0 comments

      Clear and beautiful

    • + 1 comment

      I was having initially an issue with this code because i used the sort function with the set similar to how it works for list. I love this solution but can you explain to me what is the importance of using a set? I want to know for my own knowledge and improvement.

      • + 0 comments

        Why use a set over an array is it because it cannot contain duplicates?

    • + 0 comments

      I was looking for a solution that didn't use dictionaries or marksheet. Your approach gave me a new way to look at the problem. Thank you!

    • + 0 comments

      I undertstood only this code, it is easy to read and understand with great use of sets and list with sort

    • + 0 comments

      Very neat readable code - as mentioned just anyone can understand. For a change, liked this more than the compact snippets mentioned. Even more when it comes to debugging.

      BTW, you could use the following one liner for the last code block to sort and print the names list on seperate lines-

      print(*sorted(second_lowest_names), sep='\n')

    • + 0 comments

      Elegant piece