-4
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?

ODP
  • 117
  • 2
  • 12
  • Which language are you programming in? Why don't you add it as a tag? You *have* read [the help pages](http://stackoverflow.com/help), especially the sections named ["What topics can I ask about here?"](http://stackoverflow.com/help/on-topic) and ["What types of questions should I avoid asking?"](http://stackoverflow.com/help/dont-ask). Also please [take the tour](http://stackoverflow.com/tour) and [read about how to ask good questions](http://stackoverflow.com/help/how-to-ask). Lastly please learn how to create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve). – Some programmer dude Nov 21 '17 at 23:22
  • @Someprogrammerdude Woops, forgot to add the language. How is this not a complete example? And I don't see how this fits into the category of questions which shouldn't be asked. – ODP Nov 21 '17 at 23:26
  • What you want is for `list` objects to support a fluent interface. They do not. – juanpa.arrivillaga Nov 21 '17 at 23:51

3 Answers3

2

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.

smokes2345
  • 215
  • 1
  • 12
  • Can you please have a look at the bit that I added to the end of my question? – ODP Nov 21 '17 at 23:31
  • You can use another variable or use list concatenation instead of appending – geckos Nov 21 '17 at 23:38
  • Great, thanks. @geckos how could I use list concatenation to do the same operation in this case? (Unfortunately I can't use another variable because it causes extra issues in my code.) – ODP Nov 21 '17 at 23:44
  • @ODP almost certainly, it will not. The proper way to do what you want is to save to some temporary variable, `my_temp_var = ('* '*5).split()` then use `my_temp_var.append()` and finally, `m.append(my_temp_var)`. – juanpa.arrivillaga Nov 21 '17 at 23:52
  • @juanpa.arrivillaga that worked fine in my code. Thank you so much – ODP Nov 22 '17 at 00:07
  • Just use the plus operator and another list. – geckos Nov 22 '17 at 00:17
0

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.

geckos
  • 4,912
  • 1
  • 34
  • 40
-1

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]
timgeb
  • 73,231
  • 20
  • 109
  • 138
  • OK thanks. So in this case in the middle of some code I want to return the string in question after appending an empty space instead of the "$*$", which is demonstrated in the question. So I need to return the appended list, rather than print it. If I can't do ('* '*5).split().append(" ") (because this just returns None), then what can I do? – ODP Nov 21 '17 at 23:23
  • @ODP inside a function, use `return v` instead of `print v` if you want the function to return the list. – timgeb Nov 21 '17 at 23:26
  • I've added a bit more to my question – ODP Nov 21 '17 at 23:28