3
>>> a = [1, 2, 3]
>>> a.append(4)
>>> a
[1, 2, 3, 4]

But:

>>> [1, 2, 3].append(4)
>>>

Why do list methods in Python (such as insert and append) only work with defined variables?

hopper
  • 13,060
  • 7
  • 49
  • 53
Daniil Ryzhkov
  • 7,416
  • 2
  • 41
  • 58

6 Answers6

9

In the second sample nothing is printed, because append, that was called on a list (note that append was actually performed), returns None.

Alternatively you should mention that a.append(4) also gave you a blank line (as your first sample shows), and final output of a first code sample was a representation of result of a expression, not a.append('4') expression.

Nothing is printed after append call in both cases because it is a representation of None.

Rostyslav Dzinko
  • 39,424
  • 5
  • 49
  • 62
  • 4
    @Daniil -- Why does `list.append` return `None`? Because Guido said that's what it should return ... – mgilson Aug 31 '12 at 14:20
  • 1
    @Daniil: The Python shell only displays results of the last operation if it's not `None`, which is why you didn't even see that. Try `print [1, 2, 3].append(4)` and you'll see the result of the `append()` method. – martineau Aug 31 '12 at 17:20
  • @Daniil: Also try `[1, 2, 3] + [4]` to see that you _can_ append to a literal. – martineau Aug 31 '12 at 17:22
4

list.append returns None. a.append('4') didn't print anything either since things which return None don't print anything in the interactive interpreter ...

Note that your second method call did work. It appended '4' to the list, you'll just never get to see it since you immediately lose any handle you had on the list you created.

mgilson
  • 300,191
  • 65
  • 633
  • 696
4

It does work. It just doesn't print anything, the same way that a.append(4) doesn't print anything. It's only because you saved the list as a variable that you can display its new value.

deadly
  • 1,194
  • 14
  • 24
  • 1
    **+1** - Didn't know about `_` – Eric Aug 31 '12 at 15:08
  • 1
    It's pretty handy when you're using the Python shell and you forget to assign your previous calculation to a variable. `a = _` – deadly Aug 31 '12 at 22:37
  • 1
    Also, this doesn't actually work - `_` is being populated from a previous operation in your shell – Eric Sep 16 '13 at 13:31
1

Another way to concatenate lists:

>>> [1, 2, 3] + [4]
[1, 2, 3, 4]

Note that in this case, a new list is created and returned. list.append adds an item to an existing list and does not return anything.

codeape
  • 97,830
  • 24
  • 159
  • 188
1

It's not that the list methods works only with defined variables. It's that the specified method append of list always return None while change the internal state of the list in question.

John Wang
  • 4,562
  • 9
  • 37
  • 54
0

The function only works with defined variables because that is how it is indented to work. From the python documentation:

list.append(x)

Add an item to the end of the list; equivalent to a[len(a):] = [x].

Note that is does "work" in the sense that a value is returned and the function does not raise an exception. But the results are lost because you have no reference to the modified list.


As an aside, some languages enforce naming conventions to make it explicit that a function modifies a given object. For example, Ruby and Scheme append a ! - so in this case the function might be called append!. Such a naming convention helps make the intent of the function more clear.
Justin Ethier
  • 131,333
  • 52
  • 229
  • 284