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?

7
  • Did you mean to append the string '4' rather than the number 4? Commented Aug 31, 2012 at 14:25
  • It doesn't metter in this case :) Commented Aug 31, 2012 at 14:25
  • 1
    >>> [1, 2, 3].count(2) # proves that list methods also works with not only the defined variable. Commented Aug 31, 2012 at 14:26
  • Sure, just checking it wasn't going to cause you a problem somewhere else. :) Commented Aug 31, 2012 at 14:26
  • @wrongite you're right. Edited my question Commented Aug 31, 2012 at 14:28

6 Answers 6

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.

Sign up to request clarification or add additional context in comments.

3 Comments

@Daniil -- Why does list.append return None? Because Guido said that's what it should return ...
@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.
@Daniil: Also try [1, 2, 3] + [4] to see that you can append to a literal.
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.

Comments

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.

3 Comments

+1 - Didn't know about _
It's pretty handy when you're using the Python shell and you forget to assign your previous calculation to a variable. a = _
Also, this doesn't actually work - _ is being populated from a previous operation in your shell
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.

2 Comments

What if I want to use insert() method?
This is fundamentally different than [1,2,3].append('4') though. Adding lists creates a new list, rather than adding to an existing list.
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.

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.