Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
insert
#1
hello, I'm a beginner at python and I run this code:

x = [1,2,3,4]
print("x= ",x)
y = x.insert(1,6)
print("y= ", y)
and the answer is:

Output:
x= [1, 2, 3, 4] y= None
why?
Reply
#2
list.insert() method works in-place, e.g.
my_list = [1, 2, 3, 4]
my_list.insert(1, 6)
print(my_list)
Output:
[1, 6, 2, 3, 4]
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Thanks.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020