Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   How to have a list of lists (or array of lists) (http://www.velocityreviews.com/forums/t496182-how-to-have-a-list-of-lists-or-array-of-lists.html)

 bahoo 04-03-2007 04:12 PM

How to have a list of lists (or array of lists)

Hi,

I want to have many lists, such as list0, list1, list2, ..., each one
holding different number of items.
Is there something like
list[0]
list[1]
list[2]

so that I can iterate through this list of lists?

Thanks!
bahoo

 irstas@gmail.com 04-03-2007 04:15 PM

Re: How to have a list of lists (or array of lists)

On Apr 3, 7:12 pm, "bahoo" <b83503...@yahoo.com> wrote:
> Hi,
>
> I want to have many lists, such as list0, list1, list2, ..., each one
> holding different number of items.
> Is there something like
> list[0]
> list[1]
> list[2]
>
> so that I can iterate through this list of lists?
>
> Thanks!
> bahoo

listOfLists = [[1,2], [5,7,9], [4,2,1,4,6,6]]

No problem there. The lists can contain any objects, including lists.

for x in listOfLists:
print 'list:',
for y in x:
print y,
print

 7stud 04-03-2007 06:06 PM

Re: How to have a list of lists (or array of lists)

On Apr 3, 10:12 am, "bahoo" <b83503...@yahoo.com> wrote:
> Hi,
>
> I want to have many lists, such as list0, list1, list2, ..., each one
> holding different number of items.
> Is there something like
> list[0]
> list[1]
> list[2]
>
> so that I can iterate through this list of lists?
>
> Thanks!
> bahoo

list0 = [1]
list1 = [2,3,4,5]
list2 = [6,7,8]

allLists =[list0, list1, list2]
print allLists[1][3]
print allLists[0][0]

 Bruno Desthuilliers 04-03-2007 07:37 PM

Re: How to have a list of lists (or array of lists)

bahoo a écrit :
> Hi,
>
> I want to have many lists, such as list0, list1, list2, ..., each one
> holding different number of items.
> Is there something like
> list[0]
> list[1]
> list[2]
>
> so that I can iterate through this list of lists?

listoflists = [
[1, 2, 3],
["foo", "bar", "baaz", "quux"],
["A", "B", "C", "D", "E"]
]

for alist in listoflists:
for item in alist:
print item

 All times are GMT. The time now is 10:37 AM.