How to access elemenst in a list of lists?

Terry Reedy tjreedy at udel.edu
Mon May 9 15:24:16 EDT 2011


On 5/9/2011 4:25 AM, Antonio CHESSA wrote:
> apple = [["a","b","c"],[1,2,3,4,5,6],["antony","max","sandra","sebastian"]]
>
> apple[0] = ["a","b","c"]
> apple[1] = [1,2,3,4,5,6]
> apple[2] = ["antony","max","sandra","sebastian"]
>
> apple[0][1] = "b"
> apple[2][3] = "sebastian"
>
> to view all videos in a loop so you can set:
>
> for i in range(len(apple)):
>    print apple[i]
>    for j in range (len(apple[i])):
>       print apple[i][j]

While this illustrate double indexing, it can be simplified to

for top in apple:
   print(top)
   for inner in top:
      print(inner)

and give the same output. Indexing is only needed for random access and 
for changing a list in place.

-- 
Terry Jan Reedy




More information about the Python-list mailing list