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

D

Derek Basch

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/
 
R

Ryan Paul

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]
 
L

Larry Bates

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.
 
P

Peter Abel

Derek Basch said:
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
..
..
..

An solution No. 992345739219242.... is:):
from operator import add
word = ["albert", "likes", "surfing!"]
map(add,word,['foo']*len(word)) ['albertfoo', 'likesfoo', 'surfing!foo']

Regards
Peter
 
P

Peter Otten

Derek said:
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"]

Note that the solutions presented so far do *not* modify the list in place.
This is important if you keep references to the list elswhere. For example:
backup = sample = ["alpha", "beta", "gamma"]
sample = map(lambda x: x + ".suffix", sample)
sample is backup False
sample = [x + ".suffix" for x in sample]
sample is backup
False

Here are two ways to achieve inplace modification:
backup = sample = ["alpha", "beta", "gamma"]
for i, x in enumerate(sample):
.... sample = x + ".suffix"
....
True
backup = sample = ["alpha", "beta", "gamma"]
sample[:] = [x + ".suffix" for x in sample]
sample is backup True

Here the [:] slice on the left makes the difference.

Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,044
Latest member
RonaldNen

Latest Threads

Top