Clarification of notation

B

Bruce Whealton

Hello all,
I recently started learning python. I am a bit thrown by a
certain notation that I see. I was watching a training course on
lynda.com and this notation was not presented. For lists, when would
you use what appears to be nested lists, like:
[[], [], []]
a list of lists?
Would you, and could you combine a dictionary with a list in this fashion?

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
I read that backwards?
Thanks,
Bruce
 
S

Seebs

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?

No, it's not -- it's a different use of [] to indicate that things
are optional, a convention which dates back to long before Python
existed.
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
I read that backwards?

That is exactly correct. The key is the implication that you can omit
end, or both start and end. (But you can't omit start and provide end.)

-s
 
A

alex23

Bruce Whealton said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top