index in "double" list

G

Guest

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?

B.
 
L

Larry Bates

If you have list:

t=[[1,"a"],[2,"b"],[3,"c"]]

The first element of each list contained
in t is accessed by:

t[n][0]

where n is the element number you wish to access
(zero based of course).

-Larry
 
D

Dan Bishop

Bo?tjan Jerko said:
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)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top