Josh C
Josh C

Reputation: 3

Tuples within OrderedDict

I have the following OrderedDict

activities = OrderedDict([('eating',(1,False)),('drinking',(2,True)),('breathing',(3,True)),('talking',(4,False)),('walking',(5,False))])

I want to be able to able to run a function to reverse the True/False part of a particular entry.

so far I have the following:

def updateactivity(activity):
    activities = OrderedDict([(k, (1,False)) if k == activity else (k, v) for k, v in activities.items()])

Which does well to switch the value to False. Is there a more elegant way to simply reverse the True/False in either direction?

Upvotes: 0

Views: 2746

Answers (1)

wwii
wwii

Reputation: 23773

Unpack each item's value into two variables/names then reconstruct with not.

d = collections.OrderedDict()
for k, (a,b) in activities.items():
    d[k] = (a, not b)

or

collections.OrderedDict((k,(a, not b)) for k, (a,b) in activities.items())

Upvotes: 2

Related Questions