How I can convert a Python Tuple into Dictionary?

PythonServer Side ProgrammingProgramming

A dictionary object can be constructed using dict() function. This function takes a tuple of tuples as argument. Each tuple contains key value pair.

>>> t=((1,'a'), (2,'b'))
>>> dict(t)
{1: 'a', 2: 'b'}

If you want to interchange key and value,

>>> t=((1,'a'), (2,'b'))
>>> dict((y, x) for x, y in t)
{'a': 1, 'b': 2}
raja
Published on 02-Jan-2018 23:33:50
Advertisements