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

Larry Bates lbates at swamisoft.com
Tue May 25 17:58:38 EDT 2004


Most of the time this is done without
a loop at all as follows:

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

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

word = [""albertfoo", "likesfoo", "surfing!foo"]

Much of what you use loops for in other
languages you don't need in Python.  List
comprehensions is a very powerful tool and it is
worth spending some time learning about.

Some tutorial links:

http://www.secnetix.de/~olli/Python/list_comprehensions.haw

http://www.cosc.canterbury.ac.nz/~greg/python/listcomp/


Larry Bates
Syscon, Inc.

"Derek Basch" <dbasch at yahoo.com> wrote in message
news:mailman.285.1085515057.6949.python-list at python.org...
> 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"]
>
> Thanks,
> Derek Basch
>
>
>
>
>
>
>
> __________________________________
> Do you Yahoo!?
> Friends.  Fun.  Try the all-new Yahoo! Messenger.
> http://messenger.yahoo.com/
>





More information about the Python-list mailing list