Someone at the 2015-03-06 dojo had a question about the meaning of {0[0]} and {0[1]} in format strings for specifying arguments on page 224 of Learning Python, 5th edition. So we played with it below.
In [1]:
somelist = list('SPAM')
somelist
Out[1]:
In [2]:
somelist[0]
Out[2]:
In [3]:
somelist[2]
Out[3]:
In [4]:
'first={0}, last={1}'.format(somelist[0], somelist[-1])
Out[4]:
In [5]:
'first={0[0]}, third={0[2]}, another={1}'.format(somelist, 'hello')
Out[5]:
In [6]:
somedict = {'hello': 'world', 5: 3}
somedict
Out[6]:
In [7]:
'first={0[hello]}, third={0[5]}, another={1}'.format(somedict, 'hello')
Out[7]: