Tuples within OrderedDict

2.7k views Asked by At

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?

1

There are 1 answers

3
wwii On BEST ANSWER

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())