11
#!/usr/bin/python

numbers = [1, 2, 3, 5, 6, 7]

clean = numbers.insert(3, 'four')

print clean
# desire results [1, 2, 3, 'four', 5, 6, 7]

I am getting "None". What am I doing wrong?

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
user62617
  • 1,796
  • 7
  • 18
  • 22

4 Answers4

20

Mutating-methods on lists tend to return None, not the modified list as you expect -- such metods perform their effect by altering the list in-place, not by building and returning a new one. So, print numbers instead of print clean will show you the altered list.

If you need to keep numbers intact, first you make a copy, then you alter the copy:

clean = list(numbers)
clean.insert(3, 'four')

this has the overall effect you appear to desire: numbers is unchanged, clean is the changed list.

Alex Martelli
  • 854,459
  • 170
  • 1,222
  • 1,395
  • 1
    I found the solution helpful, thank you. But I was wondering the reason of creating `clean` with `list(numbers)` instead of directly `numbers` whose type is already a list? – Francis Mar 24 '15 at 05:42
  • 1
    I did say, precisely, "If you need to keep numbers intact, first you make a copy, then you alter the copy". `numbers` is a list, but unless you make a copy, you'll *alter* that list; and it appears the OP wanted to keep the original intact while making a changed copy. – Alex Martelli Mar 24 '15 at 13:30
  • 1
    As a beginner of Python, I thought just the assignment of variable `clean` would literally make a copy of `numbers` so the following operation on `clean` would keep `numbers` intact. – Francis Mar 25 '15 at 02:34
  • 1
    @Fredom, no, that's an absolutely crucial issue in Python (and Java, and Javascript, and most other languages today): assignment does **not** make a copy, just adds one more reference to the object on the right-hand side of the `=` sign. That only matters for mutable objects, but, of course, Python lists (like Java and Javascript arrays) **are** mutable!-) – Alex Martelli Mar 25 '15 at 02:54
  • Thanks for the reminder! Though it is nothing more than foundation, I found `clean = numbers[:]` and `clean = copy(numbers)` do the same job. – Francis Mar 25 '15 at 03:29
  • @Fredom, yep, there are many ways to do a shallow copy (though to call a global named `copy` you need in addition a very peculiar import). I like the general approach typename(typeinstance) because it works in a very general way (`list`, `dict`, `set`, and many others yet) **and** even when the instance isn't "quite" of the type you're naming, e.g `list(someset)` makes a list with the same items as the set, without any problem or trouble, too. – Alex Martelli Mar 25 '15 at 03:38
11

The insert method modifies the list in place and does not return a new reference. Try:

>>> numbers = [1, 2, 3, 5, 6, 7]
>>> numbers.insert(3, 'four')
>>> print numbers
[1, 2, 3, 'four', 5, 6, 7]
Ned Deily
  • 83,389
  • 16
  • 128
  • 151
3

The list.insert() operator doesn't return anything, what you probably want is:

print numbers
RedGlyph
  • 11,309
  • 6
  • 37
  • 49
1

insert will insert the item into the given list. Print numbers instead and you'll see your results. insert does not return the new list.

Tom
  • 21,468
  • 6
  • 39
  • 44