Ramblings

March 13, 2008

More list comprehensions

Filed under: dev, one-liner, python — michaelangela @ 6:51 am

This may get mangled with ScribeFire. It was, and fixed in Zoundry’s Raven.

It’s a great example though of taking a list and changing it with a list comprehension. That last one qualifies as a one liner.

3.6. Mapping Lists (from Dive into Python)

>>> params = {“server”:”mpilgrim”, “database”:”master”, “uid”:”sa”, “pwd”:”secret”}
>>> params.items() [(‘server’, ‘mpilgrim’), (‘uid’, ‘sa’), (‘database’, ‘master’), (‘pwd’, ‘secret’)]
>>> [k for k, v in params.items()]
[‘server’, ‘uid’, ‘database’, ‘pwd’]
>>> [v for k, v in params.items()]
[‘mpilgrim’, ‘sa’, ‘master’, ‘secret’]
>>> [“%s=%s” % (k, v) for k, v in params.items()]
[‘server=mpilgrim’, ‘uid=sa’, ‘database=master’, ‘pwd=secret’]

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: