Velocity Reviews

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 03:13 PM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57