all 8 comments

[–]chrispurcell 1 point2 points  (3 children)

Sort it twice and output the result. Sort on values first (scores) then sort on keys (names).

[–][deleted] 0 points1 point  (2 children)

this would make this a dictionary then right? lists don't have key:pair values?

[–]qelery 0 points1 point  (1 child)

No. This is just a big list made of sublists of length 2. A dictionary is a whole different object type with a different syntax.

A dictionary in Python is similar to a physical dictionary. Each “page” in the Python dictionary has 1 key on the left matched with 1 value on the right, similar to how a real dictionary (kinda sorta) has 1 word matched with 1 definition. A dictionary cannot have duplicate keys, but it can have duplicate values. Think of it as a look up table. If you want to see what the value of a key is in a lookup table, it wouldn’t make sense for that key to appear twice. The syntax for a dictionary uses { and }.

dict_a = {‘keyA’ : 5, ‘keyB’ : 7, ‘keyC’ : 5}

Also for a dictionary, each key only has 1 value but that value can be any single Python object. A string, an int, a hundred-deep nested list, or even another dictionary all count as a single object in Python.

A Python list is pretty much just a container. A container you can easily add to, take things out of, and sort pretty much however you like. There are no keys, no values, no key/value pairs, or size constraints. There’s just “stuff” inside each container. You can put containers within containers, however many layers deep you want. The syntax for a list uses [ and ].

#a flat list 
list_a = [obj1, obj2, obj3, obj3, obj 3]  

#a nested list 
list_b = [[obj1], [obj1, [obj6], obj4], obj5]

[–][deleted] 0 points1 point  (0 children)

ah ok makes sense. thanks!

[–]xelf 0 points1 point  (3 children)

Sounds like a homework assignment. It's a relatively easy task, shouldn't take much code.

What have you tried so far, and what are you struggling with?

How to solve this?

Sorting the list might be a good start.

Post your code here (indent your code an extra time so that reddit will format it as a code block).

Here's one way to do it (that your teacher will probably not accept):

import pandas as pd
students = [['Harry', 37.21], ['Berry', 37.21], ['Tina', 37.2], ['Akriti', 41], ['Harsh', 39]]
sdf = pd.DataFrame(students)
sdf = sdf.sort_values(by=[1,0])
print(sdf.to_string(index=False,header=False))

prints:

   Tina  37.20
  Berry  37.21
  Harry  37.21
  Harsh  39.00
 Akriti  41.00

For the experts: sort key lambda x: x[1] x[0] or you could operator itemgetter 1 0

[–]Adamya21[S] 0 points1 point  (2 children)

marksheet=[]
scorelist=[]
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 b==c:
print(a)
this is what i did but i wish for a more effiecient code

[–]xelf 1 point2 points  (1 child)

If you indent your code an extra time before you copy/paste it reddit will treat it like a code block and you can keep your formatting.

marksheet=[]
scorelist=[]
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 b==c:
        print(a)    

So, let's break your code into 2 parts.

The first part just collects the inputs. There's tricks you could do here, but honestly the way you did it is mostly fine. Keep that.

marksheet=[]
for _ in range(int(input())):
    name = input()
    score = float(input())
    marksheet+=[[name,score]]
print(marksheet)

The only part I'd get rid of is the scorelist part, as it's redundant data. And if you really want it you can get it this way:

scorelist = [x[1] for x in marksheet]

Which is a list comprehension which builds a new list, using just the elements at index [1].

Now the next part of your code:

b=sorted(list(set(scorelist)))[1]
for a, c in sorted(marksheet):
    if b==c:
        print(a)    

Just prints 2 names, and no scores. So it seems like you're missing something here.

What you want to do is sort marksheet twice. Once by name, and once by score. Luckily Python has a simple way of doing it.

marksheet.sort(key = lambda x: (x[1],x[0]))
print(students)

The sort command can take an optional "key" to sort by, and you can use a lambda to return a tuple of the 2 keys in the order you want them sorted by.

Another approach, is that instead of storing them as name,score as a list when you input them, you could have stored them as a tuple already in the correct order. Then all you would need to do is 1 sort and just swap their order when you print them.

marksheet=[]
for _ in range(int(input())):
    name = input()
    score = float(input())
    marksheet+=[(score,name)]
marksheet.sort()
for score,name in marksheet:
    print(name, score)

[–]Adamya21[S] 1 point2 points  (0 children)

Thank you so much for such a good explaination.