Clarification of notation

Discussion in 'Python' started by Bruce Whealton, Sep 30, 2010.

  1. 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
     
    Bruce Whealton, Sep 30, 2010
    #1
    1. Advertising

  2. Bruce Whealton

    Seebs Guest

    On 2010-09-30, Bruce Whealton <> wrote:
    > 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
    --
    Copyright 2010, all wrongs reversed. Peter Seebach /
    http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
    http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
    I am not speaking for my employer, although they do rent some of my opinions.
     
    Seebs, Sep 30, 2010
    #2
    1. Advertising

  3. Bruce Whealton

    alex23 Guest

    Bruce Whealton <> 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.
     
    alex23, Sep 30, 2010
    #3
    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. Andreas H?lscher

    hex notation

    Andreas H?lscher, Dec 10, 2003, in forum: VHDL
    Replies:
    2
    Views:
    23,103
    Egbert Molenkamp
    Dec 10, 2003
  2. =?ISO-8859-1?Q?Andreas_H=F6lscher?=

    hex notation

    =?ISO-8859-1?Q?Andreas_H=F6lscher?=, Dec 10, 2003, in forum: VHDL
    Replies:
    6
    Views:
    9,491
    Jim Lewis
    Dec 12, 2003
  3. Grey Squirrel

    Hungarian Notation Vs. Pascal Notation?

    Grey Squirrel, Mar 19, 2007, in forum: ASP .Net
    Replies:
    6
    Views:
    1,335
    Steve C. Orr [MCSD, MVP, CSM, ASP Insider]
    Mar 21, 2007
  4. Tameem
    Replies:
    454
    Views:
    12,233
  5. Robert Mark Bram

    Dot notation V Bracket notation

    Robert Mark Bram, Jul 4, 2003, in forum: Javascript
    Replies:
    3
    Views:
    475
    Robert Mark Bram
    Jul 5, 2003
Loading...

Share This Page