r/learnpython • u/bahhhtam88 • Aug 05 '22
BEGINNER CODER
Hey Peeps! I'm new to python and working and working on code academy. I'm tackling a project called “Len’s Slice” where I have to .insert() [2.5, “peppers”] into an index for a sublist. Please help! I'm stumped!
0
Upvotes
3
u/ginsujitsu Aug 05 '22
Welcome to coding! Have a look at the community guidelines. Posting properly will help us help you and it keeps the quality of the sub high as well.
2
8
u/ryan770 Aug 05 '22
the syntax is
list.insert(index, element)so for:
list1 = [1, 2, 3, 4, 5]if we do this:
list1.insert(3, [2.5, 'peppers'])what we're doing is inserting a nested list at index 3 (the 4th position, indexes start at 0), and moving the rest of the elements over one.
print(list1)outputs:
[1, 2, 3, [2.5, 'peppers'], 4, 5]