<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>jathanism &#187; list comprehension</title>
	<atom:link href="http://jathan.com/tag/list-comprehension/feed/" rel="self" type="application/rss+xml" />
	<link>http://jathan.com</link>
	<description>computers, robots, and other cool things.</description>
	<lastBuildDate>Fri, 28 May 2010 21:14:06 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Python List Comprehension for Dummies</title>
		<link>http://jathan.com/2009/08/12/python-list-comprehension-for-dummies/</link>
		<comments>http://jathan.com/2009/08/12/python-list-comprehension-for-dummies/#comments</comments>
		<pubDate>Wed, 12 Aug 2009 18:59:36 +0000</pubDate>
		<dc:creator>jathan</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[list comprehension]]></category>
		<category><![CDATA[losers]]></category>
		<category><![CDATA[technical]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://jathan.com/?p=72</guid>
		<description><![CDATA[So I code a lot. I code in Python a lot. You might say I love Python. I might say you&#8217;re right. One of the most powerful things about Python is its ability to iterate over ANYTHING as if it were a list. Lists, tuples, dictionaries, and even strings can all be iterated quickly and [...]]]></description>
			<content:encoded><![CDATA[<p>So I code a lot.  I code in Python a lot.  You might say I love Python.  I might say you&#8217;re right.</p>

<p>One of the most powerful things about Python is its ability to iterate over ANYTHING as if it were a list. Lists, tuples, dictionaries, and even strings can all be iterated quickly and elegantly.  Python also introduces a concept known as <a href="http://docs.python.org/tutorial/datastructures.html#list-comprehensions" target="_blank">list comprehension</a> which allows you to do rather complex filtering of list contents within a single statement.</p>

<p>To illustrate how awesome and powerful list comprehension is, let&#8217;s start with a basic example that is NOT using it:
<pre>&gt;&gt;&gt; my<em>list = [1,2,3,4,5]
&gt;&gt;&gt; for item in my</em>list:
...     if item % 2 == 0: print item, 'is an even number.'
...
2 is an even number.
4 is an even number.</pre>
So, let&#8217;s assume that we want to identify all even numbers inside of <code>my<em>list</em></code>, and put them into a new list called <code>evens</code> the old-fashoned way:
<pre>&gt;&gt;&gt; mylist = [1,2,3,4,5]
&gt;&gt;&gt; evens = []
&gt;&gt;&gt; for item in my<em>list:
...     if item % 2 == 0: evens.append(item)
...
&gt;&gt;&gt; evens
[2, 4]</em></pre>
<strong>Why the old-fashioned way sucks</strong>
First things first, the empty list called <code>evens</code> had to be declared ahead of time.  This is because when we looped thru the list called <code>mylist</code> using the <code>for</code> statement, when the if test is performed on each <code>item</code> we have to reference <code>evens</code> by name to <code>append()</code> the even numbers to it.</p>

<p><strong>Why list comprehension rocks</strong>
With list comprehension, the logic that isolates the even numbers and the declaration of the list that will capture this output are compressed into a single statement:
<pre>&gt;&gt;&gt; my<em>list = [1,2,3,4,5]
&gt;&gt;&gt; evens = [i for i in my</em>list if i % 2 == 0 ]
&gt;&gt;&gt; evens
[2, 4]</pre>
The logic is encapsulated in <em>[square brackets]</em> indicating that the output will be a list.  The list comprehension itself is the logic between the brackets that determines what will be in the list that it spits out.</p>

<p>So list comprehensions at their most basic level allow for compression of code and streamlining of logical statements. Advanced usage of list comprehension can get pretty silly, but then so can nested loop statements.  It supports nesting as many statements as you can throw at it so longs as they are syntactically correct.</p>

<p>If you find yourself coding shit like this:
<pre>&gt;&gt;&gt; losers = ['Joe','Jim','Jon','Jen']
&gt;&gt;&gt; for u in losers:
...     if u.startswith('J'):
...             if u.endswith('n'):
...                     if u != 'Jon':
...                             print u
...
Jen</pre>
Then maybe list comprehension is for you:
<pre>&gt;&gt;&gt; [u for u in losers if u.startswith('J') and u.endswith('n') and u != 'Jon']
['Jen']</pre>
No offense to anyone named Joe, Jim, or Jon.</p>
]]></content:encoded>
			<wfw:commentRss>http://jathan.com/2009/08/12/python-list-comprehension-for-dummies/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
