0

I'm very confused about a situation. Why I cannot assign a list to a variable. Can someone explain it to me?

a =[1,2,3,4]
b = a.insert(0,1)
print(b)

the output is

None
Ben Wang
  • 99
  • 3
  • 10

2 Answers2

4

The insert() method only inserts the element to the list. It doesn't return any value.

Jaydip Rakholiya
  • 792
  • 10
  • 20
0

Adding to the replies that you got, if it is the list b that needs to be updated but not the list a, you should proceed as such:

a =[1,2,3]
b = list(a)
b.insert(1,20)
print(a, b)
Matina G
  • 1,452
  • 2
  • 14
  • 28