## Understanding list comprehension

A forum for general discussion of the Python programming language.

### Understanding list comprehension

Code: Select all
`cubes_by_four=[x**3 for i in range(1,11) if x**3 %4==0 ]printprint cubes_by_four`

I was just experimenting with the list comprehension code. The above code gives the output
[1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000]
I am unable to understand this. How exactly does list comprehension work ?
Last edited by metulburr on Fri Jan 24, 2014 7:35 am, edited 1 time in total.
Reason: initial post lock
surya

Posts: 4
Joined: Fri Jan 24, 2014 7:06 am

### Re: Understanding list comprehension

Code: Select all
`cubes_by_four=[x**3 for i in range(1,11) if x**3 %4==0 ]`

the first x**3 gets appended to the list, only if the if condition rings true throughout the for loop

that is the equivalent to the basic for loop of:
Code: Select all
`x = 10cubes_by_four = []for i in range(1,11):    if x**3 % 4 == 0:        cubes_by_four.append(x**3)print(cubes_by_four)`
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam

metulburr

Posts: 1838
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

### Re: Understanding list comprehension

I imagine you probably meant this:
Code: Select all
`>>> [x**3 for x in range(1,11) if x**3 %4==0 ][8, 64, 216, 512, 1000]>>> `

As your code currently stands x never changes. It is equal to whatever you set it to before you generated the list comprehension.
Code: Select all
`>>> x = 10>>> [x**3 for i in range(1,11) if x**3 %4==0 ][1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000]>>> `

-Mek
• Use code tags when posting code.
• Include any errors with your post (in code tags).
• Make examples the minimum length to demonstrate your issue.

Mekire

Posts: 1500
Joined: Thu Feb 07, 2013 11:33 pm
Location: Tucson, Arizona

### Re: Understanding list comprehension

@Mekire: Thank you for your response but I meant what I wrote. I was playing around with the syntax and wanted to know if changing the variable within the list had any kind of effect. I had previously thought of a case of the value of the variable acting in a similar way as pass by reference.
The code I provided does not assign the value 10 to x anywhere.SO how does x assume it ?
Last edited by surya on Fri Jan 24, 2014 10:08 am, edited 1 time in total.
surya

Posts: 4
Joined: Fri Jan 24, 2014 7:06 am

### Re: Understanding list comprehension

I'm sorry but no, that isn't what happens. You previously defined x in your interactive session even if you didn't realize it, I'm guessing.

This is what happens if you don't define x:
Code: Select all
`>>> [x**3 for i in range(1,11) if x**3 %4==0 ]Traceback (most recent call last):  File "<interactive input>", line 1, in <module>NameError: name 'x' is not defined>>> `

-Mek
• Use code tags when posting code.
• Include any errors with your post (in code tags).
• Make examples the minimum length to demonstrate your issue.

Mekire

Posts: 1500
Joined: Thu Feb 07, 2013 11:33 pm
Location: Tucson, Arizona

### Re: Understanding list comprehension

I did not as a matter of fact I am working with the exercises on Codeacademy and they provide a clean slate.
In the code that you have provided ,you have forgotten to assign the list comprehension to a list.
so it should be:
Code: Select all
`cubes_by_four=[x**3 for x in range(1,11) if x**3 %4==0 ]`
It is possible that somehow your IDE is giving some error.I use Pythonwin and it is running the program without any error.
surya

Posts: 4
Joined: Fri Jan 24, 2014 7:06 am

### Re: Understanding list comprehension

I'm afraid I have forgotten nothing here.

Codecademy is giving an incorrect result here and it should actually be reported to them.

Please run the code with an actual python interpreter:

This code:
Code: Select all
`cubes_by_four=[x**3 for i in range(1,11) if x**3 %4==0 ]printprint cubes_by_four`

Saved and executed, will result in this:
Code: Select all
`Traceback (most recent call last):  File "<module1>", line 1, in <module>NameError: name 'x' is not defined`

-Mek

Edit:
I realize what is happening with codecademy here. The previous excercise before the one you cited is solved with the code:
Code: Select all
`even_squares = [x**2 for x in range(2,11,2)]`
This leaves x when you start the next excercise with a value of, you guessed it, 10.

The repl does not reset between exercises. This is bad.
• Use code tags when posting code.
• Include any errors with your post (in code tags).
• Make examples the minimum length to demonstrate your issue.

Mekire

Posts: 1500
Joined: Thu Feb 07, 2013 11:33 pm
Location: Tucson, Arizona

### Re: Understanding list comprehension

That is indeed very much possible.Thanks and if I were to sum it up,if I took out what codeacademy did and try to run the code ,it will give an error.Right?
surya

Posts: 4
Joined: Fri Jan 24, 2014 7:06 am

### Re: Understanding list comprehension

Yes, variables always need to be defined before they are used.

When you do something like:
Code: Select all
`for i in range(5):    print(i**2)print(i)`
It is important to note that even though there is no equal sign, i is still being assigned each time through the loop; and after the loop finishes still retains the last value it had.

Demonstrated with list comprehensions here:
Code: Select all
`>>> xTraceback (most recent call last):  File "<interactive input>", line 1, in <module>NameError: name 'x' is not defined>>> [x**2 for x in range(5)][0, 1, 4, 9, 16]>>> x4>>> `

This is a good example of why functions have local namespaces and why keeping namespaces clean is important in general. An error should never pass silently because a variable had a value we didn't expect it to.

-Mek
• Use code tags when posting code.
• Include any errors with your post (in code tags).
• Make examples the minimum length to demonstrate your issue.

Mekire

Posts: 1500
Joined: Thu Feb 07, 2013 11:33 pm
Location: Tucson, Arizona