How to manipulate elements of a list in a single line of code?

Paulo da Costa pmc411-usenet at yahoo.com
Mon Feb 25 16:46:45 EST 2008


mrstephengross wrote:
> 1  list1 = ['a', 'b', 'c']
> 2  list2 = []
> 3  for item in list1: list2.append(item + 'foo')
> 
> Is there a way to express lines 2-3 sort-of ilke this:
> 
>   list2 = [ for item in list1: item + 'foo' ]

list2 = [ item + 'foo' for item in list1 ]

Paulo



More information about the Python-list mailing list