Oh, no! The second wave is coming, and you need to gather supplies for another extended quarantine. You quickly make a LIST of essential items to buy at Target:
>>> my_supplies = ['hand sanitizer', 'toilet paper', 'face masks', 'spam', 'board games', 'gummy bears']
Your mom is concerned when she sees how quickly your neighbors are buying up toilet paper, and she wants to grab as much as possible before it totally runs out. She asks you to add another pack of toilet paper to your list.
>>> my_supplies.append('toilet paper')
>>> print(my_supplies)
['hand sanitizer', 'toilet paper', 'face masks', 'spam', 'board games', 'gummy bears', 'toilet paper']
Your younger brother has been making his own list. He wants to add his list to the end of yours.
>>> bro_supplies = ['pasta', 'instant coffee', 'toilet paper', 'nintendo']
>>> my_supplies.extend(bro_supplies)
>>> print(my_supplies)
['hand sanitizer', 'toilet paper', 'face masks', 'spam', 'board games', 'gummy bears', 'toilet paper', 'pasta', 'instant coffee', 'toilet paper', 'nintendo']
After you get your brother’s list, your mom drives you to Target, only to find that it has been closed in order to maintain social distancing guidelines. Instead, you are greeted by a shopping robot.
“Good day, my name is Bulls-Eye,” he introduces himself. “In order to accommodate the needs of all our customers, I would like you to give me a list of items you want, and I will sent one of my minions to fetch them in the order they appear on the list.”
You hand Bulls-Eye my_supplies (in its state up to this point) and ask, “When will I get my gummy bears?”
>>>my_supplies.index('gummy bears')
5
Bulls-Eye also warns you, “Due to unexpected demand, we have a strict limit of 2 packs of toilet paper per customer. I will need to check how many packs you are requesting.”
>>> my_supplies.count('toilet paper')
3
"I'm sorry, that's too much toilet paper. When this happens, I will have to remove it every time it appears in your list until you reach the limit."
EXERCISE: Write a method that Bulls-Eye could use to accomplish this task. You will need a while loop and the method list.remove(x), which removes the first appearance of x from the list.
"Well, that's too long to wait for toilet paper," your mom grumbles, only to be distracted by her phone going off. She checks it, and turns to you with a concerned look on her face.
"I have bad news, your dad has been given a pay cut, and now we can't afford everything on your list."
EXERCISE: You'll have to delete items from your list by calling my_supplies.pop(), which returns the item to be removed. When you call this for the first time, what item will be removed?
Meanwhile, Bulls-Eye says that customers can call sorted(list) if they want to receive their items in alphabetical order, or reverse(sorted(list)) to receive them in reverse alphabetical order.
EXERCISE: If you sort my_supplies before submitting your list for purchase, which item will be returned second, or at index 1? What if you reverse your list after removing the last item, but do not sort in alphabetical order? What if you do both?
HW: Write a program that removes the last element from the list by calling .pop() until the element that has been removed is 'toilet paper'.