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

Discussion in 'Python' started by Derek Basch, May 25, 2004.

  1. Derek Basch

    Derek Basch Guest

    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/
     
    Derek Basch, May 25, 2004
    #1
    1. Advertising

  2. Derek Basch

    Ryan Paul Guest

    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]
     
    Ryan Paul, May 25, 2004
    #2
    1. Advertising

  3. Derek Basch

    Larry Bates Guest

    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" <> wrote in message
    news:...
    > 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/
    >
     
    Larry Bates, May 25, 2004
    #3
  4. Derek Basch

    Peter Abel Guest

    Derek Basch <> wrote in message news:<>...
    > 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
     
    Peter Abel, May 26, 2004
    #4
  5. Derek Basch

    Peter Otten Guest

    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 = 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
     
    Peter Otten, May 26, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gogo
    Replies:
    1
    Views:
    2,109
    Sudsy
    Sep 4, 2003
  2. Brendan

    Alter list items within loop

    Brendan, Jun 11, 2009, in forum: Python
    Replies:
    4
    Views:
    349
    Lie Ryan
    Jun 15, 2009
  3. thunk
    Replies:
    1
    Views:
    323
    thunk
    Mar 30, 2010
  4. thunk
    Replies:
    0
    Views:
    496
    thunk
    Apr 1, 2010
  5. -berlin.de
    Replies:
    0
    Views:
    145
    -berlin.de
    Jun 30, 2007
Loading...

Share This Page