Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: pythonic way

Thread Tools

Re: pythonic way

Chris Angelico
Posts: n/a
On Fri, Nov 2, 2012 at 7:58 PM, jack <(E-Mail Removed)> 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'

['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.

Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Object Oriented vs Pythonic Code, and Pythonic standards Carl J. Van Arsdall Python 4 02-07-2006 10:15 PM
best Pythonic way to do this sort: Python newb Sean Berry Python 6 10-10-2005 01:29 PM
Controlling a generator the pythonic way Thomas Lotze Python 12 06-14-2005 12:56 AM
Pythonic way to do static local variables? Charles Krug Python 11 04-27-2005 06:01 AM
What's the Pythonic way to do this? Doug Rosser Python 4 09-12-2004 03:24 PM