Game Development Reference
In-Depth Information
9 - Hangman
Lists of Lists
Lists are a data type that can contain other values as items in the list. But these items can
also be other lists. Let's say you have a list of groceries, a list of chores, and a list of your
favorite pies. You can put all three of these lists into another list. Try typing this into the
shell:
>>> groceries = ['eggs', 'milk', 'soup', 'apples',
'bread']
>>> chores = ['clean', 'mow the lawn', 'go grocery
shopping']
>>> favoritePies = ['apple', 'frumbleberry']
>>> listOfLists = [groceries, chores,
favoritePies]
>>> listOfLists
[['eggs', 'milk', 'soup', 'apples', 'bread'],
['clean', 'mow the lawn', 'go grocery shopping'],
['apple', 'frumbleberry']]
>>>
You could also type the following and get the same values for all four variables:
>>> listOfLists = [['eggs', 'milk', 'soup',
'apples', 'bread'], ['clean', 'mow the lawn', 'go
grocery shopping'], ['apple', 'frumbleberry']]
>>> groceries = listOfLists[0]
>>> chores = listOfLists[1]
>>> favoritePies = listOfLists[2]
>>> groceries
['eggs', 'milk', 'soup', 'apples', 'bread']
>>> chores
['clean', 'mow the lawn', 'go grocery shopping']
>>> favoritePies
['apple', 'frumbleberry']
>>>
To get an item inside the list of lists, you would use two sets of square brackets like this:
listOfLists[1][2] which would evaluate to the string 'go grocery
shopping' . This is because listOfLists[1] evaluates to the list ['clean',
'mow the lawn', 'go grocery shopping'][2] . That finally evaluates to
'go grocery shopping' .
Here is another example of a list of lists, along with some of the indexes that point to the
items in the list of lists named x . The red arrows point to indexes of the inner lists
Search WWH ::




Custom Search