[Tutor] List comprehensions

Kent Johnson kent37 at tds.net
Wed Mar 23 12:30:35 CET 2005


Liam Clarke wrote:
> 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)

You can nest list comps. Except for the pop, the above can be written
   a = [ [ int(item) for item in i] for i in x ]
which is pretty readable.

There is probably some way to get the pop in there too but I avoid list comps with side effects as 
bad style.

Kent



More information about the Tutor mailing list