Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialKody Dibble
2,554 PointsHow to use insert and pop
Not sure exactly how to use the .pop and .insert syntax together.
the_list = ["a", 2, 3, 1, False, [1, 2, 3]]
# Your code goes below her
the_list.pop[0].insert(1)
1 Answer
Chris Freeman
Treehouse Moderator 68,441 Pointsassert()
tasks two arguments: the insert position and the item to insert. pop()
takes one argument: the position of the item to be removed. pop()
return the popped item.
# pop the 1 from the list. It's in the 4th position so use index 3 in 0-based counting
popped_item = the_list.pop(3)
# insert it at the beginning of the list. Beginning is index 0
the_list.insert(0, popped_item)
# combining the statements above into one
the_list.insert(0, the_list.pop(3))
Andy Manning
628 PointsAndy Manning
628 PointsThanks, this made it super clear for me - noob question but why not use [] to describe the index inside the insert and pop methods? I wanted to as that's how describing indexes has been taught so far, is there a logical rule/reason I can remember as to why?