Clarification of notation

alex23 wuwei23 at gmail.com
Thu Sep 30 06:29:58 CEST 2010

```Bruce Whealton <br... at futurewavedesigns.com> wrote:
> For lists, when would
> you use what appears to be nested lists, like:
> [[], [], []]
> a list of lists?

Well, you'd use it when you'd want a list of lists ;)

There's nothing magical about a list of lists, it's just a list with
objects inside like any other, in this case they just happen to be
lists. Possibly the canonical example is for a simple multidimensional
array:

>>> array5x5 = [[0]*5 for i in range(5)]
>>> array5x5[2][3] = 7
>>> array5x5[4][1] = 2
>>> array5x5
[[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 7, 0],
[0, 0, 0, 0, 0],
[0, 2, 0, 0, 0]]

> Would you, and could you combine a dictionary with a list in this fashion?

Lists can contain dictionaries that contain dictionaries containing
lists :) So yes, they can easily be combined.

Here's a list with dictionaries:

elements = [
{'tag': 'strong', 'style': 'bold'},
{'tag': 'header', 'style': 'bolder', 'color': 'red},
]

And a dictionary of lists:

classes_2010 = {
'economics': ['John Crowley', 'Jack Savage', 'Jane Austen'],
'voodoo economics': ['Ronald Reagan', 'Ferris Beuller'],
}

(Note that the formatting style is a personal taste and not
essential).

Generally, you tend to use a list when you want to work on items in
sequence, and a dictionary when you want to work on an item on demand.

> Next, from the documentation I see and this is just an example (this
> kind of notation is seen elsewhere in the documentation:
>
> str.count(sub[, start[, end]])
> This particular example is from the string methods.
> Is this a nesting of two lists inside a a third list?  I know that it
> would suggest that some of the arguments are optional, so perhaps if
> there are 2 items the first is the sub, and the second is start?  Or did

In documentation (as opposed to code), [] represents optional
arguments, and have nothing at all to do with Python lists. The above
example is showing that the method can be called in the following
ways:

'foobarbazbam'.count('ba')
'foobarbazbam'.count('ba', 6)
'foobarbazbam'.count('ba', 6, 9)

Hope this helps.

```