Addressing the last element of a list

Bengt Richter bokr at oz.net
Tue Nov 8 04:55:50 EST 2005


On 8 Nov 2005 01:12:07 -0800, "pinkfloydhomer at gmail.com" <pinkfloydhomer at gmail.com> wrote:

>I knew there was an easy way :)
>
>Just to satisfy my curiousity: Is there a way to do something like the
>reference solution I suggest above?
>

If the last element in the list was a dict, then you could do something like

 >>> lst = ['the', 'last', 'element of this list is a dict', {'key':"key's value"}]
 >>> ref = lst[-1]
 >>> ref
 {'key': "key's value"}
 >>> ref["foo"] =42
 >>> ref['bar'] = 'Ni!'
 >>> ref
 {'foo': 42, 'bar': 'Ni!', 'key': "key's value"}
 >>> ref['key']
 "key's value"
 >>> ref['bar']
 'Ni!'
 >>> del ref['key']
 >>> ref
 {'foo': 42, 'bar': 'Ni!'}

FWIW ;-)

Regards,
Bengt Richter



More information about the Python-list mailing list