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 trial

Python Python Collections (2016, retired 2019) Lists Shopping List Take Three

diego cortes
diego cortes
1,421 Points

position = 0 I've assigned position =2, position = 8 and it works the same, assigning items to desired order. Why???

i am just trying to make sense of why did we assign position = 0, if any other number doesn't seem to affect the insert(position-1, new_item) part of the code

1 Answer

Jonathan Malin
Jonathan Malin
9,546 Points

The position = 0 part of the code is only run when the list is empty. Anytime the list has items in it, the position for value is set in the if block directly above the else statement and the else block is ignored. The logic I see behind position = 0 is that we need position to have a value before we use it or we will get an error for trying to use a variable that hasn't been created yet and setting position = 0 makes it easier for us to read and understand where we want to place the new item in the list(not that we really have a choice since the list is empty the first time through, it can just make it easier to follow). When the insert function is used with the empty list, it doesn't care what number it is passed for placement because there is only one spot it can place it in the list.