V
V
vtaeke2020-08-18 15:17:50
Python
vtaeke, 2020-08-18 15:17:50

How to remove the last item in the list on output, after removing duplicate items?

Without pastrami: Using the sandwich_orders list from Exercise 7-8, make sure that the value 'pastrami' appears at least three times in the list. Add code to the top of the program to print a message that there are no more pastrami, and write a while loop to remove all occurrences of 'pastrami' from sandwich_orders. Make sure that finished_sandwiches does not contain the value 'pastrami' even once.

I'm not catching up a bit on how to make a program so that all 'pastrami' elements are deleted.

sandwich_orders = ['bokaldilo', 'arepa', 'kebab', 'pastrami', 'pastrami', 'pastrami', 'pastrami']
finished_sandwiches = []

while sandwich_orders:
    current_sandwich = sandwich_orders.pop()

    if 'pastrami' in sandwich_orders:
        sandwich_orders.remove('pastrami')
        print("Пастрами больше нет")

    print("Изготовлен сэндвич: " + current_sandwich.title())
    finished_sandwiches.append(current_sandwich)

print("\nГотовы следующие сэндвечи:")
for finished_sandwich in finished_sandwiches:
    print(finished_sandwich.title())

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Drill, 2020-08-18
@Drill

finished_sandwiches = [dish  for dish in sandwich_orders if dish != 'pastrami']

print(finished_sandwiches )

In [1]:
['bokaldilo', 'arepa', 'kebab']

If a while loop is required :
while 'pastrami' in sandwich_orders:
    sandwich_orders.pop(sandwich_orders.index('pastrami'))

print(sandwich_orders)

In [2]:
['bokaldilo', 'arepa', 'kebab']

M
Maxim, 2020-08-18
@Tomio

finished_sandwich = list(filter(lambda item: item != 'pastrami', sandwich_orders))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question