In [1]:
from collections import namedtuple
In [2]:
City = namedtuple('City', 'name country population coordinates')
tokyo = City('Tokyo', 'JP', 36.933, (35.689722, 139.691667))
In [3]:
tokyo
Out[3]:
In [4]:
tokyo.population
Out[4]:
In [5]:
tokyo.coordinates
Out[5]:
In [6]:
tokyo[1]
Out[6]:
In [7]:
City._fields
Out[7]:
In [8]:
LatLong = namedtuple('LatLong', 'lat long')
In [9]:
delhi_data = ('Delhi NCR', 'IN', 21.935, LatLong(28.613889, 77.208889))
In [10]:
delhi = City._make(delhi_data)
In [11]:
delhi
Out[11]:
In [12]:
delhi._asdict
Out[12]:
In [13]:
delhi._asdict()
Out[13]:
In [14]:
for key, value in delhi._asdict().items():
print(key + ':', value)