By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,172 Members | 762 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,172 IT Pros & Developers. It's quick & easy.

modifying values of List or Dictionary when iterating on them

P: n/a
Hello

I just want to update the data inside List or Dictionary without
adding or deleting object.

is this correct ?

l=[1, 2, 3]
for i, v in enumerate(l):
l[i]=v+1

d=dict(a=1, b=2, c=3)
for k, v in d.iteritems():
d[k]=d[k]+1

Both works, but :

are they correct ?
are they optimum for big structure ?

Thanks

May 24 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
d=dict(a=1, b=2, c=3)
for k, v in d.iteritems():
d[k]=d[k]+1
You might as well do: d[k] = v + 1, like for the list.
May 24 '07 #2

P: n/a
On 24 mai, 19:21, "Christopher Anderson" <sidewinder....@gmail.com>
wrote:
d=dict(a=1, b=2, c=3)
for k, v in d.iteritems():
d[k]=d[k]+1

You might as well do: d[k] = v + 1, like for the list.

ops, yes it was a typo

May 24 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.