Python: Is it possible to have a list of lists? and how do I call a particular list?

Here is what I am thinking:

MSILISTS = [
    AC_LIST = [
        'somename.msi',
        'someothername.msi',
    ]
]

i am not sure if this is right...

but the problem is how do I set it up with lots of these individual lists and then call them?

Answers


Make MSILISTS a dict.

>>> MSILISTS = {}
>>> MSILISTS['AC_LIST'] = [
...         'somename.msi',
...         'someothername.msi',
...     ] 

>>> MSILISTS['AC_LIST']
['somename.msi', 'someothername.msi']

You either want to use nested lists:

MSILISTS = [
    [
        'somename.msi',
        'someothername.msi',
    ],
]

print MSILISTS[0]

Or use a dictionary:

MSILISTS = {
    'AC_LIST': [
        'somename.msi',
        'someothername.msi',
    ]
}

print MSILISTS['AC_LIST']

If you want to have named lists you could have MSILISTS be a dictionary:

MSILISTS = {"AC_LIST":['somename.msi','someothername.msi']}

If you just want nested, unnamed lists that is also fine:

MSILISTS = [['somename.msi','someothername.msi']]

You can have a list of lists, but in that situation the lists are not named. You must refer to them by index number, or iterate over them. Example:

 my_list = [ [1, 3, 4], ['A', 'B', 'C'], ['you', 'me'] ]

 print my_list[1]
 ['A', 'B', 'C']

 for list in my_list:
    print list

 [1, 3, 4]
 ['A', 'B', 'C']
 ['you', 'me']

You can also have a dictionary of lists, in which case you can name each list (but lose the ordering of the lists within the dictionary):

 my_dictionary = { 'number' : [1, 3, 4], 'letters' : ['A', 'B', 'C'], 'words' : ['you', 'me'] }

 print my_dictionary['letters']
 ['A', 'B', 'C']

Note that in each case the structures of the sub-lists don't have to be the same or hold the same contents, and some of the items in the larger list or dictionary can be things other than lists as well.


Yes, you can have list of lists, but your code is not valid Python code. The following would work:

MSILISTS = [ 
    [ 
        'somename.msi', 
        'someothername.msi', 
    ],
    [ 'some more data']
] 

The inner list is just a normal element of the outer one, so you can access it via MSILISTS[0]. If you want to access the list by name, you have to use a dictionary:

MSILISTS = {  
    'AC_LIST' : [  
        'somename.msi',  
        'someothername.msi',  
    ]  
}

Now you can access the inner list via MSILISTS['AC_LIST'].


You can indeed have lists inside of lists, like

a=[0,1,2,['a','b','c']]

to get a value from the list inside a, reference it using it's index >>> in my example marks a line you type, while lines without it are output.

>>> a[3]
['a','b','c']
>>> a[3][1]
'b'

you can not have a name for a list inside a list like this:

a=[0,1,b=[2,3]]

This is because this makes no sense, what would b point to, from where could you access it? You can however associate objects, in the form of key/value pairs in a type called dict. The literal for a dict looks like this:

a={'a':'b',2:'b','c':2}

you can use any hashable object as a key. That means any object that can be converted to an int using it's hash method, but strings and ints are the most common dictionary keys. You can then reference a value like you would do in a list, but instead of specifying an index, you specify a key.

>>> a['a']
'b'
>>> a[2]
'b'

Yes, these are just multi-dimensional lists.

If you really want to push performance with these, I suggest using a module called "numpy".

Here is how you would instantiate a list of lists:

>>> my_list = ['t','o','f','u']

>>> blah = [my_list,['b','a','g','s'],['b','a','n','a','n','a','s'],['planes','trains','automobiles']]
>>> blah[1]
['b', 'a', 'g', 's']
>>> blah[2]
['b', 'a', 'n', 'a', 'n', 'a', 's']
>>> blah[3]
['planes', 'trains', 'automobiles']
>>> blah[1][1]
'a'
>>> blah[3][1]
'trains'


etc. etc.

Need Your Help


About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.