data structuresalgorithmsand code simplification the collections module contains function namedtuple(that is used to create subclasses of tuple in which attribute names can be used to access tuple elements namedtuple(typenamefieldnames [verbose]creates subclass of tuple with name typename fieldnames is list of attribute names specified as strings the names in this list must be valid python identifiersmust not start with an underscoreand are specified in the same order as the items appearing in the tuple--for example['hostname','port'alternativelyfieldnames can be specified as string such as 'hostname portor 'hostnameportthe value returned by this function is class whose name has been set to the value supplied in typename you use this class to create instances of named tuples the verbose flagif set to trueprints the resulting class definition to standard output here is an example of using this functionfrom collections import namedtuple networkaddress namedtuple('networkaddress',['hostname','port'] networkaddress('www python org', hostname 'www python orga port hostport len( type(aisinstance(atupletrue in this examplethe named tuple networkaddress isin every wayindistinguished from normal tuple except for the added support of being able to use attribute lookup such as hostname or port to access tuple components the underlying implementation is efficient--the class that is created does not use an instance dictionary or add any additional memory overhead in built-in tuple all of the normal tuple operations still work named tuple can be useful if defining objects that really only serve as data structures for exampleinstead of defining classlike thisclass stock(object)def _init_ (self,name,shares,price)self name name self shares shares self price price you could define named tuple insteadimport collections stock collections namedtuple('stock','name shares price'both versions are going to work in nearly identical manner for examplein either caseyou would access fields by writing names sharesand so on howeverthe benefit of the named tuple is that it is more memory-efficient and supports various tuple operations such as unpacking (for exampleif you had list of named tuplesyou could unpack values in for-loop with statement such as for namesharesf lib fl ff