Comprehension Expressions

A place where you can post Python-related tutorials you made yourself, or links to tutorials made by others.

Comprehension Expressions

Postby micseydel » Fri Feb 22, 2013 1:40 am

Comprehensions are syntactic sugar for for loops. Here is code to create a list without a comprehension
Code: Select all
squares = []
for x in xrange(10):
    squares.append(x**2)

Three lines for something simple like that? Not in Python! Here's the same thing as a list comprehension
Code: Select all
squares = [x**2 for x in xrange(10)]

What about if we want to exclude certain x values?
Code: Select all
squares = [x**2 for x in xrange(10) if x % 2 != 0]

This works for odd values up to 100.

Nested values are possible as well, and can be used to make two or more dimensional lists
Code: Select all
>>> two_dimensional = [[x for x in xrange(y*3, y*3 + 3)] for y in xrange(3)]
>>> for row in two_dimensional:
   print row

   
[0, 1, 2]
[3, 4, 5]
[6, 7, 8]

What if we had the two_dimensional variable and we wanted to flatten it into a one dimensional one with list comprehension?
Code: Select all
>>> [element for row in two_dimensional for element in row]
[0, 1, 2, 3, 4, 5, 6, 7, 8]

We typically think of reading comprehensions backwards as compared to loops.

Not only are there list comprehensions, but there are comprehensions for generators, sets and dictionaries as well. Here is the syntax
Code: Select all
>>> squares_set = {x**2 for x in xrange(10)}
>>> squares_set
set([0, 1, 4, 81, 64, 9, 16, 49, 25, 36])
>>>
>>> squares_dict = {x: x**2 for x in xrange(10)}
>>> squares_dict
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81}
>>>
>>> squares_generator = (x**2 for x in xrange(10))
>>> squares_generator
<generator object <genexpr> at 0x7f9cfc0485f0>
>>> sum(squares_generator)
285
>>> sum(squares_generator)
0
>>> sum(x for x in xrange(10))
45

Note that the set and dictionary syntax is very similar, and that a generator, once exhausted is empty. If this behavior is puzzling, you should review generators, which are their own topic.

Feedback always appreciated!
Last edited by micseydel on Fri Feb 22, 2013 3:19 am, edited 1 time in total.
Reason: Removed excess code from an example
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1291
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Comprehension Expressions

Postby Mekire » Fri Feb 22, 2013 2:28 am

Question: I'm incredibly lazy. Can I print with a list comprehension?

Answer: Well yes you can Mr. Lazy-Guy.

In Python 3 print is a function so this is extremely simple:
Code: Select all
[print("So lazy!! "*i) for i in range(5)]

In Python 2.x this takes a little more work because print is a statement:
Code: Select all
from __future__ import print_function
[print("So lazy!! "*i) for i in xrange(5)]

The output of both will be:
Code: Select all
So lazy!!
So lazy!! So lazy!!
So lazy!! So lazy!! So lazy!!
So lazy!! So lazy!! So lazy!! So lazy!!

Anyway. Stop being so lazy.

-Mek
(I personally love doing this, but as previously admitted, I love one-liners.
User avatar
Mekire
 
Posts: 987
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: Comprehension Expressions

Postby micseydel » Fri Feb 22, 2013 3:14 am

If you want a one-liner for your terminal session, I don't know why you don't just write
Code: Select all
for i in range(5): print "lazy"

but as long as you don't put it into any real code, ever, that's fine.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1291
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Comprehension Expressions

Postby Mekire » Fri Feb 22, 2013 3:45 am

Admittedly with the example I gave that can be done.
There would have to be an additional for or if statement within it to justify the one-liner.
Of course yes, it isn't really acceptable code, but I have always enjoyed the novelty that it can be done.

-Mek
User avatar
Mekire
 
Posts: 987
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan


Return to Tutorials

Who is online

Users browsing this forum: No registered users and 3 guests