Why can't I append a list formed from split()?
Question:
v = ('* '*5).split()
print v, len(v), type(v)
print v.append(5)
Output:
['*', '*', '*', '*', '*'] 5 <type 'list'>
None
Why does an attempt to append v cause an output of None? As demonstrated, v is a list type of length 5, but still append does not seem to work.
What can I do to append to v?
My exact case
I have a list of lists:
m = [[1, 2, 3, 4],[5, 6, 7, 8],[1, 2, 3, 4]]
m.append(('* '*5).split().append(" "))
This won’t work because I am trying to append None to m.
How can I return this appended list instead of printing it, so that I return the list rather than None?
Answers:
list.append
mutates the list (the change is performed in-place) and returns None
. print v
in order to see the change.
>>> v = ('* '*5).split()
>>> v.append(5)
>>> print v
['*', '*', '*', '*', '*', 5]
append
change the list in place. Is a convention in python that methods that do in place editing return None
So what you are seeing is the return of append operation. If you print the list afterwards you’ll see the change.
In your example you are printing the return value of list.append
, however this method does not return a value. append
manipulates the original copy of the list. It does not create a new list or return the list.
Thus you need to do your append first, then print the new list.
v = ('* '*5).split()
print v, len(v), type(v)
v.append(5)
print v
Your code will append to v, but you need to append first, then print, in two separate statements.
In this situation it may be helpful to understand the difference between ‘mutable’ and ‘immutable’ types. Mutable types are those which you can modify in place and include things like integers, lists, and dictionaries. When working with lists and dictionaries you can modify them in place by adding new elements using the append
or update
methods. There is no need for these methods to return a new object because they directly mutate the object they are associated with.
However, strings represent an immutable type. Whenever you assign a value to a string you create a whole new string object, every time. This is the reason why strings don’t have an ‘append’ method.
v = ('* '*5).split()
print v, len(v), type(v)
print v.append(5)
Output:
['*', '*', '*', '*', '*'] 5 <type 'list'>
None
Why does an attempt to append v cause an output of None? As demonstrated, v is a list type of length 5, but still append does not seem to work.
What can I do to append to v?
My exact case
I have a list of lists:
m = [[1, 2, 3, 4],[5, 6, 7, 8],[1, 2, 3, 4]]
m.append(('* '*5).split().append(" "))
This won’t work because I am trying to append None to m.
How can I return this appended list instead of printing it, so that I return the list rather than None?
list.append
mutates the list (the change is performed in-place) and returns None
. print v
in order to see the change.
>>> v = ('* '*5).split()
>>> v.append(5)
>>> print v
['*', '*', '*', '*', '*', 5]
append
change the list in place. Is a convention in python that methods that do in place editing return None
So what you are seeing is the return of append operation. If you print the list afterwards you’ll see the change.
In your example you are printing the return value of list.append
, however this method does not return a value. append
manipulates the original copy of the list. It does not create a new list or return the list.
Thus you need to do your append first, then print the new list.
v = ('* '*5).split()
print v, len(v), type(v)
v.append(5)
print v
Your code will append to v, but you need to append first, then print, in two separate statements.
In this situation it may be helpful to understand the difference between ‘mutable’ and ‘immutable’ types. Mutable types are those which you can modify in place and include things like integers, lists, and dictionaries. When working with lists and dictionaries you can modify them in place by adding new elements using the append
or update
methods. There is no need for these methods to return a new object because they directly mutate the object they are associated with.
However, strings represent an immutable type. Whenever you assign a value to a string you create a whole new string object, every time. This is the reason why strings don’t have an ‘append’ method.