At the very end of the video where you show us the issue with using the remove method while looping through confused me.
From what I know when you remove something from an array in java everything gets shifted to the left 1 so therefore if you remove something on index 0 unless you subtract 1 from the current index you are going to remove it will skip over what was just shifted down.
But when you copied the inventory and then removed from the master inventory I got very confused as to why that works.
Kyle Petran5,017 Points
Okay well in Python, everything to the right of the element you removed gets shifted to the left one as you stated. However I believe while he is looping through his List the item index is remains the same. It'll be easier to demonstrate.
so we start just as he started:
inventory = ["apple", "orange", "pizza", "burger"] for item in inventory: inventory.remove(item)
Now logically our item at index 0 is removed:
inventory = ["orange", "pizza", "burger"]
here is where I think you were tripped up. Instead of removing the foremost element, the item variable now goes from inventory(which is currently "orange") to inventory(which is currently "pizza") so the result it:
inventory = ["orange", "burger"]
If this isn't clear please let me know.
thanks for the great answer
really make sense. Thanks