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)

1st May 2020, 2:42 AM
Anwyn Eisinger
Anwyn Eisinger - avatar
3 Answers
+ 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.
1st May 2020, 2:46 AM
Eswar V
+ 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)
1st May 2020, 2:45 AM
Anwyn Eisinger
Anwyn Eisinger - avatar
+ 1
the syntax of insert function is list.insert(position,value) So,in your code you are inserting 10 at position 2
1st May 2020, 5:31 AM
ANJALI SAHU