The 'collections' module has some interesting methods that give us an edge over generic inbuilt tools . I want to explore them here today and show how you can use them .
namedtuple
Tuples are represented as (item1, item2, item2, ...) . They support indexing and one of the ways to use them is :
>>> from collections import namedtuple >>> Car = namedtuple('Car','Price Mileage Colour Class') >>> xyz = Car(Price = 100000, Mileage = 30, Colour = 'Cyan', Class = 'Y') >>> print xyz Car(Price=100000, Mileage=30, Colour='Cyan', Class='Y') >>> print xyz.Class Y
They turn tuples into convenient containers for simple tasks. With namedtuples, you don’t have to use integer indices for accessing members of a tuple.