Permalink
Browse files

Correct nested list comprehension example and update last change date.

  • Loading branch information...
1 parent bc8bea5 commit 64700adcc6e647fe1e7f26fb8290542f396c1ada @cgroner committed Dec 1, 2011
Showing with 13 additions and 5 deletions.
  1. +13 −5 _posts/2011-11-09-An-Introduction-to-List-Comprehensions-in-Python.textile
@@ -2,7 +2,7 @@
layout: post
title: An Introduction to List Comprehensions in Python
category: [Python]
-last_changed: 2011-11-10
+last_changed: 2011-12-01
---
<div class="darken-comments">
@@ -106,8 +106,8 @@ Neat, clear and concise.
h2. Loops of loops.
-Finally, it's worth mentioning that lists can be nested
-in list comprehensions.
+Finally, it's worth mentioning that you can use list comprehensions
+to iterate on more than one list. For example:
{% highlight python %}
>>> list_a = ['A', 'B']
@@ -117,8 +117,16 @@ in list comprehensions.
{% endhighlight %}
Just like you would expect in @for@ loops, the last
-loop moves the fastest. Also note that using nested list
-comprehensions returns a list of @tuples@.
+loop moves the fastest. Also note that this method returns
+a list of @tuples@. If you'd like nested lists, you can
+also nest one list comprehension within another.
+
+{% highlight python %}
+>>> list_a = ['A', 'B']
+>>> list_b = ['C', 'D']
+>>> [[x+y for x in list_a] for y in list_b]
+[['AC', 'BC'], ['AD', 'BD']]
+{% endhighlight %}
h2. Summary.

0 comments on commit 64700ad

Please sign in to comment.