Re: pythonic way

Discussion in 'Python' started by Chris Angelico, Nov 2, 2012.

  1. On Fri, Nov 2, 2012 at 7:58 PM, jack <> wrote:
    > thanks,but I don't think enumerate() is my want
    > Have some ways to operate the reference of element,not a copy when I tried
    > to traverse a list?
    >
    > I'm so sorry about my poor English, hope you don't mind it.


    No probs, I'll be a little less vague and pointer-y and give you some
    example code. :)

    lst = ['foo', 'bar', 'quux', 'asdf', 'qwer', 'zxcv']
    for idx, val in enumerate(lst):
    if val[1]=='w': lst[idx]='Replaced'
    print(lst)

    ['foo', 'bar', 'quux', 'asdf', 'Replaced', 'zxcv']


    Does that explain it a bit better? You get the index and can then
    mutate the list using that index, thus replacing the original entry.

    ChrisA
    Chris Angelico, Nov 2, 2012
    #1
    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. Doug Rosser

    What's the Pythonic way to do this?

    Doug Rosser, Sep 10, 2004, in forum: Python
    Replies:
    4
    Views:
    323
    Phillip J. Eby
    Sep 12, 2004
  2. Charles Krug
    Replies:
    11
    Views:
    550
    Bengt Richter
    Apr 27, 2005
  3. Thomas Lotze

    Controlling a generator the pythonic way

    Thomas Lotze, Jun 11, 2005, in forum: Python
    Replies:
    12
    Views:
    446
    Thomas Lotze
    Jun 14, 2005
  4. Sean Berry
    Replies:
    6
    Views:
    319
    George Sakkis
    Oct 10, 2005
  5. Carl J. Van Arsdall
    Replies:
    4
    Views:
    488
    Bruno Desthuilliers
    Feb 7, 2006
Loading...

Share This Page