-1

I have a question regarding python. So I have this list

x = [a,b,c,d,e,f,g,h,i,j]

and the function

def insert(values):
    return values.insert(3,"Hey")

print(insert(x))

basically what I'm trying to do is to insert the word Hey into the fourth index, however, I just get "None" as a reply when I run the code, and I'm not quite sure what the issue is. I'm pretty new to python and programming in general

R. Arctor
  • 718
  • 4
  • 15
  • 1
    `List.insert` doesn't return the a new list. Instead it modifies the list. So instead try something like `insert(x)` then `print(x)`. – R. Arctor Jan 21 '21 at 17:37

2 Answers2

1

The insert function of a List doesn't return anything, hence you get None. See docs for more details.

You can try this:

values = ['foo', 'bar']
values.insert(1, 'hey')
print(values)

Output:

['foo', 'hey', 'bar']
PApostol
  • 2,152
  • 2
  • 11
  • 21
0

The function list.insert modifies the list in place and always returns None. This is by design. Your approach is simply printing out the return value, which is always 'None'. To see the effect of your insert, perform the insert in one statement and then print the list in a separate statement.

Basil
  • 659
  • 4
  • 11