alter / modify a list as well as iterate on its items / objects

Ryan Paul segphault at sbcglobal.net
Tue May 25 17:47:06 EDT 2004


On Tue, 25 May 2004 12:57:32 -0700, Derek Basch wrote:

> Hello, 
> 
> Can anyone point me towards an article or explain
> how to properly alter a list as well as iterate
> on its items? For example:
> 
> input:
> 
> word = ["albert", "likes", "surfing!"]
> 
> for word in sentence:
>     word += "foo"
> 
> result:
> 
> word = ["albertfoo", "likesfoo", "surfingfoo"]
> 

word = ["albert","likes","surfing!"]

you could do it with a map:

word = map(lambda x: x + "foo",  words)

or with a list comprehension:

word = [x + "foo" for x in words]



More information about the Python-list mailing list