Convert data to dictionary in Python

Convert data to dictionary with dict function

We can use the dict function to construct dictionaries from (key, value) pairs


items = [('name', 'Gumby'), ('age', 42)]
d = dict(items)
print d
print d['name']

The code above generates the following result.

dict can be used with keyword arguments.


d = dict(name='Gumby', age=42)
print d
fdict = dict((['x', 1], ['y', 2]))
print fdict

The code above generates the following result.





















Home »
  Python »
    Data Types »




Data Types
String
String Format
Tuple
List
Set
Dictionary