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

Jimmy Otis
Jimmy Otis
3,228 Points

Someone please help explain what Kenneth means at the end of the video where he says....

"You could shorten the function a bit though by setting position to negative one instead and then just always using Insert."

as opposed to setting position to None.

Can someone please explain how to do this??

Would really be appreciated Thank you!!

2 Answers

Steven Parker
Steven Parker
231,275 Points

I believe he's talking about this part of the code:

    except ValueError:
        position = None
    if position is not None:
        shopping_list.insert(position-1, item)
    else:
        shopping_list.append(item)

And it sounds like he's suggesting this replacement:

    except ValueError:
        position = -1
    shopping_list.insert(position-1, item)

But that won't actually work, and it's not consistent with the documentation for "insert". But this would be:

    except ValueError:
        position = len(shopping_list) + 1
    shopping_list.insert(position-1, item)
Jimmy Otis
Jimmy Otis
3,228 Points

Steven, this code

 except ValueError:
        position = 0
    shopping_list.insert(position-1, item)

still gives the result where the item is placed at the -2 index spot. I can't for the life of me figure out why. Any ideas?

Steven Parker
Steven Parker
231,275 Points

Apparently, "insert" does allow negative index values, but it still places the new item in front of the indexed item. As "-1" represents the last item, the new one will become the second to last. Using the length as the index (as the documentation suggested) seems to be the only equivalent of "append".

Jimmy Otis
Jimmy Otis
3,228 Points

wow, well that explains it then. Thanks buddy

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,457 Points

Good question!

ignore this answer

The first bit to understand is the following two lines are equivalent:

shopping_list.insert(-1, item)
shopping_list.append(item)

This means the insert can be used in place of the append.

Kenneth implies a single insert statement could be used for both the user desired position or a default value. Since we need to subtract 1 from the user supplied value to get the correct index, then the default value should be set such that after subtracting 1 it has the default append index of -1.

Kenneth's point taken literally would not work because setting the position to -1 instead of None would result in an index of -2 when used in the statement

shopping_list.insert(position - 1, item)

If he has said "set the default position value such that it results in a -1 index when used in the insert statement " it would be more accurate.

I would set the default position to 0 instead of None then the "position - 1" would yield the correct index.

Post back if you need more help. Good luck!!!

Steven Parker
Steven Parker
231,275 Points

Good to know negative values are allowed, but -1 is not equivalent to "append".
The documentation says "a.insert(len(a), x) is equivalent to a.append(x)"

Jimmy Otis
Jimmy Otis
3,228 Points

Thank you Chris, I really appreciate this answer. I understand that shopping_list.insert(-1, item) shopping_list.append(item) are equivalent. However I'm still having a hard time replacing the code with 'None' and getting it to function correctly. : /

Jimmy Otis
Jimmy Otis
3,228 Points

never mind I get it now, thank you!

Jimmy Otis
Jimmy Otis
3,228 Points

just thought i'd let you know

 except ValueError:
        position = 0
    shopping_list.insert(position-1, item)

still for some reason gives the result at the -2 index value

Chris Freeman
Chris Freeman
Treehouse Moderator 68,457 Points

Yep. You are correct. I didn't test my solution correctly. I'll probably delete it so it doesn't confuse others.