On 22/08/18 17:27, Mats Wichmann wrote: > I'm really unfond of accessing members of a collection by numeric index. > > >>> numer, denom = d["twothirds"] > >>> print(numer, denom) > (2, 3) > > I think that's nicer than: numer = d["twothirds][0]
You can alsao avoid indexes with the namedtuple class in the collections module: >>> import collections as coll >>> frac = coll.namedtuple("Fraction", ['num','den']) >>> frac <class '__main__.Fraction'> >>> f = frac(2,3) >>> f.num 2 >>> f.den 3 >>> fracs = {0.67:f} >>> fracs[0.67].num 2 >>> Or just use a nested dictionary: fracs = {0.67, {'num':2, 'den':3}} print( fracs[0.67]['num'] ) But you have the inconvenience of quoting the field names. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor