list behavior

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rene Aguirre

    list behavior

    Hello everybody,

    I needed to add a list to a dictionary, something very simple:[color=blue][color=green][color=darkred]
    >>> d = {}
    >>> l = []
    >>> l.append(1)
    >>> d["one"] = l
    >>> d[/color][/color][/color]
    {'one': [1]}

    Buen, when I just move the 'append' statement to the dictionary
    assigment then:[color=blue][color=green][color=darkred]
    >>> l = []
    >>> d = {}
    >>> d["one"] = l.append(1)
    >>> d[/color][/color][/color]
    {'one': None}

    Not what I expected, then I came to the conclusion that
    [].append(value) returns 'None', why?

    Rene A.
  • anton muhin

    #2
    Re: list behavior

    Rene Aguirre wrote:[color=blue]
    > Hello everybody,
    >
    > I needed to add a list to a dictionary, something very simple:
    >[color=green][color=darkred]
    >>>>d = {}
    >>>>l = []
    >>>>l.append( 1)
    >>>>d["one"] = l
    >>>>d[/color][/color]
    >
    > {'one': [1]}
    >
    > Buen, when I just move the 'append' statement to the dictionary
    > assigment then:
    >[color=green][color=darkred]
    >>>>l = []
    >>>>d = {}
    >>>>d["one"] = l.append(1)
    >>>>d[/color][/color]
    >
    > {'one': None}
    >
    > Not what I expected, then I came to the conclusion that
    > [].append(value) returns 'None', why?
    >
    > Rene A.[/color]

    append is what is called procedure in other languages.

    d["one"].append(1) should work

    hth,
    anton.

    Comment

    Working...