[Tutor] Lists in lists

Andrei project5 at redrival.net
Sat Sep 16 16:23:22 CEST 2006


Morten Juhl Johansen wrote:
> # Newbie warning
> I am making a timeline program. It is fairly simple.
> I base it on appending lists to a list.
> Ex.
> [[year1, "headline1", "event text1"], [year2, "headline2", "event text2"]]
> 
> This seemed like a brilliant idea when I did it. It is easy to sort.
> Now, if I want to OUTPUT it, how do I indicate that I want to extract
> first entry in a list in a list? How do I print the separate entries?

Just append the position indicators, e.g. "print MyList[0][1]" will take 
item #0 in MyList and request its item #1. It's equivalent to saying

   MySubList = MyList[0]
   print MySubList[1]

In an interactive session:

 >>> li = [[1,2], [3,4]]
 >>> li[0]
[1, 2]
 >>> li[0][0]
1
 >>> li[0][1]
2
 >>> li[1][1]
4


Yours,

Andrei



More information about the Tutor mailing list