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

Don't understand position = None part and position - 1 part.

Hi so @ around 8:30 you can see the code I'm talking about. He has

position = none

and a little further down he uses

shopping_lisy.insert(position-1, item)

**I don't understand the use of position-1 and position = none.. what are their significances and uses?

2 Answers

Josh Keenan
Josh Keenan
19,652 Points

So there is and if statement running and basically if the position is NOT none, then you have to insert the thing to the list, but lists start at position 0 remember, so you have to take one away to insert the thing in the correct position.

So basically position = 0 would be come position = -1? How would that help?

Josh Keenan
Josh Keenan
19,652 Points

The way it works is that you want to insert something to the 3rd position, so if you insert it to a list using position = 3, it will be at position number 4. This is why it is -1, so it goes in the right place, given the variable is called position you can assume that it should start with a value of 1 and never be below that.

Xavi Domingo
Xavi Domingo
2,311 Points

I reckon that he wrote the code considering the user would choose position = 1 as the starting position and not position = 0. On the other hand, if there's nothing in the list, it doesn't matter whether the position becomes -1 or 0 because there's only one item. Finally, None is something we use when the user presses enter while an input is running, therefore nothing is saved on the variable. Hope it helps!