How to use list as key of dictionary?

Davy zhushenli at gmail.com
Tue Nov 6 09:50:27 CET 2007


Hi Matimus and Boris,

Thank you :)

And a further question about vector above rank 1, how can I use it as
the key of dictionary?

For example, if I have list like L=[[1,2,3],[4,5,6,7]],
Then I do L_tuple = tuple(L)
>>> L_tuple = ([1,2,3],[4,5,6,7])
But {L_tuple:'hello'} cause an error?

Best regards,
Davy

On Nov 6, 3:09 pm, Matimus <mccre... at gmail.com> wrote:
> On Nov 5, 10:53 pm, Davy <zhushe... at gmail.com> wrote:
>
> > Hi all,
>
> > We know that list cannot be used as key of dictionary. So, how to work
> > around it?
>
> > For example, there is random list like l=[1,323,54,67].
>
> > Any suggestions are welcome!
>
> > Best regards,
> > Davy
>
> Use a tuple instead.
>
> >>> d = {}
> >>> d[tuple([1,2,3,4])] = 'hello world'
> >>> d
>
> {(1, 2, 3, 4): 'hello world'}>>> d[1,2,3,4]
>
> 'hello world'
>
> Matt





More information about the Python-list mailing list