Bruce
Bruce

Reputation: 35223

Python : updating values in a dictionary

I have the following dictionary ->

key : (time,edge_list)

Now I want to increment all time values by 1. How do I do that?

dict_list = dict(key:(time+1,edge_list) for key:(time,edge_list) in dict_list)

Upvotes: 3

Views: 6718

Answers (2)

John La Rooy
John La Rooy

Reputation: 304137

>>> d={"key" : (100,"edge_list")}
>>> for i,(time,edge_list) in d.items():
...  d[i] = time+1, edge_list
... 
>>> d
{'key': (101, 'edge_list')}

Upvotes: 8

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798526

dict((key, (time + 1, edge_list)) for (key, (time, edge_list)) in somedict.iteritems())

Upvotes: 6

Related Questions