0
Question About Python Lists
Why does this code only insert 11 into the list nums instead of inserting both 2 and 11 into the list nums? nums = [9, 8, 7, 6, 5] nums.append(4) nums.insert(2, 11) print(nums)
3 ответов
+ 1
The code..
nums.insert(2,11) inserts only 11 because insert takes two parameters one is place of inserting and the value.
.insert(place of value, value)
That's why it take 11 and places it in index value of 2.
+ 1
Nevermind, I figured it out. It is because the 2 is referring to the index of 11 (aka where to insert the number 11)
+ 1
the syntax of insert function is list.insert(position,value)
So,in your code you are inserting 10 at position 2