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

Discussion in 'Python' started by bahoo, Apr 3, 2007.

  1. bahoo

    bahoo Guest

    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
     
    bahoo, Apr 3, 2007
    #1
    1. Advertising

  2. bahoo

    Guest

    On Apr 3, 7:12 pm, "bahoo" <> 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
     
    , Apr 3, 2007
    #2
    1. Advertising

  3. bahoo

    7stud Guest

    On Apr 3, 10:12 am, "bahoo" <> 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]
     
    7stud, Apr 3, 2007
    #3
  4. 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
     
    Bruno Desthuilliers, Apr 3, 2007
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==

    List of lists of lists of lists...

    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==, May 8, 2006, in forum: Python
    Replies:
    5
    Views:
    413
    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==
    May 15, 2006
  2. yomgui

    list of lists of lists ....

    yomgui, Jul 28, 2006, in forum: Python
    Replies:
    6
    Views:
    317
    yomgui
    Jul 31, 2006
  3. Mufasa
    Replies:
    0
    Views:
    369
    Mufasa
    Dec 2, 2007
  4. antar2
    Replies:
    2
    Views:
    396
    Bighead
    Jul 17, 2008
  5. Alex van der Spek
    Replies:
    2
    Views:
    2,561
    Robert Kern
    Sep 18, 2011
Loading...

Share This Page