Steven Matthews
Steven Matthews

Reputation: 11285

Insert gives me an empty list? What is going on here?

>>> numlist = ['0', '1', '2', '3', '4', '5', '6']
>>> numlist = numlist.insert(0, '-1')
>>> numlist
>>> print numlist
None
>>>

I don't get it - I am trying to append to the first position of the list, and it is giving me a NoneType?

Upvotes: 3

Views: 159

Answers (4)

CyLiu
CyLiu

Reputation: 401

Just use

numlist.insert(0, '-1')

list.insert will return a None

Upvotes: 2

Lewis Norton
Lewis Norton

Reputation: 7151

Try numlist.insert(0, '-1') without the assignment.

Upvotes: 2

Maxim Egorushkin
Maxim Egorushkin

Reputation: 136266

list.insert returns None, not a list.

Upvotes: 6

orlp
orlp

Reputation: 117691

list.insert modifies the list in-place and returns None. Use it like this instead:

>>> numlist = ['0', '1', '2', '3', '4', '5', '6']
>>> numlist.insert(0, '-1')
>>> numlist
['-1', '1', '2', '3', '4', '5', '6']

Also, is there any particular reason you are using quoted numbers?

Upvotes: 9

Related Questions