views:

30

answers:

1

For the tuple, t = ((1, 'a'),(2, 'b')) dict(t) returns {1: 'a', 2: 'b'}

Is there a good way to get {'a': 1, 'b': 2} (keys and vals swapped)?

I'm wanting to be able to return 1 given 'a' or 2 given 'b', perhaps converting to a dict is not the best way.

+2  A: 

Try:

>>> t = ((1, 'a'),(2, 'b'))
>>> dict((y, x) for x, y in t)
{'a': 1, 'b': 2}
Greg Hewgill