[Tutor] List comprehensions

Ryan Davis ryan at acceleration.net
Wed Mar 23 16:15:53 CET 2005


I'm not sure if you can or want to do this solely one list comprehension.

You could make a helper function and use map:

###
def helper(i):
    i.pop(3)
    return map(int, i)

a = map(helper, x)
###

Description of map:
http://docs.python.org/lib/built-in-funcs.html 

I think map is a little cleaner is some cases.  Not sure if its more Pythonic, I'm still trying to figure out exactly what that
means.

The need to pop(3) off the list makes a pure functional solution kinda hard.

Thanks,
Ryan 

-----Original Message-----
From: tutor-bounces at python.org [mailto:tutor-bounces at python.org] On Behalf Of Liam Clarke
Sent: Wednesday, March 23, 2005 5:44 AM
To: Tutor Tutor
Subject: [Tutor] List comprehensions

Hi, 

Is there a way to apply multiple actions within one list comprehension?

i.e. instead of 

a = []
for i in x:
    i.pop(3)
    g = [ int(item) for item in i]
    a.append(g)

Is there any guides to this (possibly obtuse) tool?

Regards, 

Liam Clarke

PS I can see how nested list comprehensions can quickly lose readability. 

-- 
'There is only one basic human right, and that is to do as you damn well please.
And with it comes the only basic human duty, to take the consequences.
_______________________________________________
Tutor maillist  -  Tutor at python.org
http://mail.python.org/mailman/listinfo/tutor



More information about the Tutor mailing list