[Tutor] Help understanding list comprehensions

Anubhav Yadav anubhav1691 at gmail.com
Tue Jun 16 17:45:01 CEST 2015


I have a doubt.

I had a list like this

[['Varun', 19.0], ['Kakunami', 19.0], ['Harsh', 20.0], ['Beria', 20.0],
['Vikas', 21.0]]

I am using a for loop to del the rows with the lowest value like this:

for row in marks:
    if row[1] == lowest:
        marks.remove(row)

But after running this loop, I get the following:

[['Kakunami', 19.0], ['Harsh', 20.0], ['Beria', 20.0], ['Vikas', 21.0]]

If I print row for every iteration one row is missed in the iteration. Here
is the output.

['Varun', 19.0]
['Harsh', 20.0]
['Beria', 20.0]
['Vikas', 21.0]

If I use list comprehensions, I get the right output. I just want to
understand what is happening. Can anyone help me out here?

This if from an hackerrank assignment.


More information about the Tutor mailing list