[Tutor] Help understanding list comprehensions

Anubhav Yadav anubhav1691 at gmail.com
Wed Jun 17 10:58:54 CEST 2015


Either the subject is misleading or you misunderstand something. Im am
sorry to tell you the great truth, but there was no list comprehension  in
your code at all, just a list. Comprehension is what Alan wrote for you,
that is the next step in studying Python, when you already understand lists
and loops well.


I understand that my title was a little misleading, and I apologise for the
same. Here is code that worked:

marks = []
for i in range(int(input())):
    name = raw_input()
    score = float(raw_input())
    marks.append([name, score])
marks = sorted(marks, key=lambda score:score[1])
lowest = marks[0][1]
marks = [ x for x in marks if x[1] != lowest]
second_lowest = marks[0][1]
lowest = sorted([x for x in marks if x[1]==second_lowest])
for row in lowest:
    print row[0]

And it worked because I was using list comprehensions for removing the
element from the list. It didn't worked in the for loop before. I wanted to
ask why it didn't worked in for loop but worked in list comprehension.


More information about the Tutor mailing list