473,387 Members | 1,834 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

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/

Jul 18 '05 #1
4 2446
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]
Jul 18 '05 #2
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/...rehensions.haw

http://www.cosc.canterbury.ac.nz/~greg/python/listcomp/
Larry Bates
Syscon, Inc.

"Derek Basch" <db****@yahoo.com> wrote in message
news:ma*************************************@pytho n.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/

Jul 18 '05 #3
Derek Basch <db****@yahoo.com> wrote in message news:<ma*************************************@pyth on.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

..
..
..

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
Jul 18 '05 #4
Derek Basch wrote:
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[i] = x + ".suffix"
.... sample is backup 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
Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Jess Austin | last post by:
hi, I like the way that Python does lists, and I love the way it does iterators. But I've decided I don't like what it does with iterators of lists. Lists are supposed to be mutable sequences,...
3
by: Thorsten Kampe | last post by:
I found out that I am rarely using tuples and almost always lists because of the more flexible usability of lists (methods, etc.) To my knowledge, the only fundamental difference between tuples...
8
by: Sorin Sandu | last post by:
Is it posible to modify the lenghth of an array ? I want to do something like int i = 0; string debit = new string; string credit = new string; while (myread3.Read())
2
by: Bob | last post by:
Hi, I have a list of widgets. I want to iterate through the selected items collection and modify one of the widgets properties. I tried foreach(widget w in lst.SelectedItems) w.MyProperty =...
10
by: pamelafluente | last post by:
Hi I have a sorted list with several thousands items. In my case, but this is not important, objects are stored only in Keys, Values are all Nothing. Several of the stored objects (might be a...
15
by: Macca | last post by:
Hi, My app needs to potentially store a large number of custom objects and be able to iterate through them quickly. I was wondering which data structure would be the most efficient to do this,a...
1
by: Danny Liberty | last post by:
I need some opionions on an issue here... Suppose I want to keep a collection of objects, each need to be uniquely identified by a number. This number has no meaning as long as it's unique, so it...
4
by: dustin.getz | last post by:
consider the following working loop where Packet is a subclass of list, with Packet.insert(index, iterable) inserting each item in iterable into Packet at consecutive indexes starting at index. ...
4
RMWChaos
by: RMWChaos | last post by:
The next episode in the continuing saga of trying to develop a modular, automated DOM create and remove script asks the question, "Where should I put this code?" Alright, here's the story: with a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.