Report offensive comment

theearthboundkid - 2007/07/04 10:51:53

Cross post from reddit.<pre><code>List comprehensions are wonderful, but some of these examples are
less clear than they could be.

>>> [a.lower() for a in [b[i] for b in x for i in range(len(b))]]
['h', 'e', 'l', 'l', 'o', 'w', 'o', 'r', 'l', 'd', 'h', 'o', 'w', 'a', 'r', 'e', 'y', 'o', 'u', '?']

would be clearer as

>>> [char.lower() for word in mylist for char in word]

9 times out of 10, you don't need to use range or len for your list
comprehensions.

One cool trick is to use generators instead of list comprehensions.
They're almost the same, but generators use parentheses instead of
brackets. The advantage of generators is that you can pass them to
functions or loops without wasting time building up a list object that
will then be GC'd one line down. Here's something cool you can do
with a generator:

>>> any(mytest(i) for i in mylist)

This will tell you if any member of your list meets your test criteria.
Since it uses a generator instead of a list comprehension, it stops
testing as soon as it gets back an item that tests true. A list
comprehension would go all the way through the list even if it
matched on the first test. Here's another good one:

>>> mycounter = sum(1 for i in mylist if cond(i))

This will tell you the number of items in the list that match your
condition. It's the same as:

>>> mycounter = 0
>>> for i in mylist:
... if cond(i):
... mycounter = 1
...

OK, finally, here are some insane list comprehensions from the
Python cookbook:

# sum
nl = [2, 3, 6, 18]
[j for j in [0] for i in nl for j in [j i]][-1]

# product
nl = [2, 3, 6, 18]
[j for j in [1] for i in nl for j in [j * i]][-1]

# factorial
fac = 6
[j for j in [1] for i in xrange(2, fac 1) for j in [j*i]][-1]

While you should never use these in production code (too cryptic!),
they are really insanely cool.</code></pre>

By clicking on the Submit button, you have deemed the user comment above as abusive or offensive. The message will be flagged and our editors will be alerted.

If you wish to be contacted, please complete the form below. Your details will be kept confidential.

Name:
E-mail:
 
 

Log in


Sign up | Forgot your password?

  • Staff XP stays on life support for longer

    This week's Roundup looks at Microsoft's decision to extend the life of Windows XP, the release of Microsoft Surface SDK, Firefox's new Geode plug-in, Yahoo's new tool -- Smush It and more. Read more »

    -- posted by Staff

  • Chris Duckett The good and truly awful celluloid depictions of computers

    Ever wonder why your lawyer uncle leaves the room whenever you turn over to Boston Legal? Or why your forensic science cousin can't stand crime drama? You know the answer: it’s the horrid trivialisation and dumbing down of an occupation to make it appear entertaining. Sometimes it is so unbelievable that it actually hurts and yelling at the screen is the only outlet. Read more »

    -- posted by Chris Duckett

  • Brendon Chase Apple's iPhone engineers to tour Sydney, Melbourne

    Aussie developers will be able to get up close and personal with some of the iPhone engineers in November to learn how to build applications for the platform. Read more »

    -- posted by Brendon Chase

What's on?