Home > Back-end >  What does the [1] do in this python snippet?
What does the [1] do in this python snippet?

Time:11-12

b=sorted(list(set(scorelist)))[1]

I was unable to solve a question and was looking up solutions but almost all of the solutions had this step. I'm confused wanted to know what the [1] does in this case. Full code:

marksheet=[]
scorelist=[]
if __name__ == '__main_':

    for _ in range(int(input())):

            name = input()

            score = float(input())

            marksheet =[[name,score]]

            scorelist =[score]

    b=sorted(list(set(scorelist)))[1] 

    for a,c in sorted(marksheet):
         if c==b:
                print(a)

Thanks in advance!

CodePudding user response:

it grabs the second item in the sorted scorelist and assigns it to 'b'.

If socrelist is [97,55,78,88] then b will equal 78

This is because 55 will be the "first" item with a place of 0

Since lists run [0, 1, 2, 3, ...]

  • Related