index in "double" list

Dan Bishop danb_83 at yahoo.com
Tue Feb 3 13:56:12 EST 2004


Bo?tjan Jerko <bostjan.jerko at mf.uni-lj.si> wrote in message news:<mailman.1154.1075795507.12720.python-list at python.org>...
> Hello!
> 
> I have a list of lists (e.g. [[1,"a"],[2,"b"],[3,"c"]]) and 
> would like to do index on the first element of the lists.
> Can it be done?

I think you might want to try using dicts.

>>> d = {1: 'a', 2: 'b', 3: 'c'}
>>> d[2]
'b'

If you already have a list of lists, you can convert it to a dict with
the constructor.

>>> a = [[1, "a"], [2, "b"], [3, "c"]]
>>> d = dict(a)



More information about the Python-list mailing list