Understanding list comprehension

A forum for general discussion of the Python programming language.

Understanding list comprehension

Postby surya » Fri Jan 24, 2014 7:27 am

Code: Select all
cubes_by_four=[x**3 for i in range(1,11) if x**3 %4==0 ]
print
print 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

Postby metulburr » Fri Jan 24, 2014 7:33 am

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 = 10
cubes_by_four = []
for i in range(1,11):
    if x**3 % 4 == 0:
        cubes_by_four.append(x**3)
print(cubes_by_four)
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1375
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Understanding list comprehension

Postby Mekire » Fri Jan 24, 2014 7:41 am

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
User avatar
Mekire
 
Posts: 986
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: Understanding list comprehension

Postby surya » Fri Jan 24, 2014 10:05 am

@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

Postby Mekire » Fri Jan 24, 2014 10:08 am

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
User avatar
Mekire
 
Posts: 986
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: Understanding list comprehension

Postby surya » Fri Jan 24, 2014 10:10 am

I did not as a matter of fact :D I am working with the exercises on Codeacademy and they provide a clean slate.
Here is the link http://www.codecademy.com/courses/python-beginner-en-KAgt5/1/3?curriculum_id=4f89dab3d788890003000096
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

Postby Mekire » Fri Jan 24, 2014 10:16 am

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 ]
print
print 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.
User avatar
Mekire
 
Posts: 986
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: Understanding list comprehension

Postby surya » Fri Jan 24, 2014 7:12 pm

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

Postby Mekire » Sat Jan 25, 2014 4:37 am

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
>>> x
Traceback (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]
>>> x
4
>>>


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
User avatar
Mekire
 
Posts: 986
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan


Return to General Discussions

Who is online

Users browsing this forum: No registered users and 1 guest

cron