0

I'd like to know the difference between the following two codes using while and for loop in each.

I want to pop elements of the list sandwich_orders into the list of finished_sandwich. it seems to work with while loop but not with for loop. What mistake did I make?

sandwich_orders = ['chicken sandwich', 'beef sandwich', 'avocado sandwich', 'pork sandwich']
finished_sandwich = []

while sandwich_orders:
    cur_sandwich = sandwich_orders.pop()
    finished_sandwich.append(cur_sandwich)
print (finished_sandwich)

for cur_sandwich in sandwich_orders:
    cur_sandwich = sandwich_orders.pop()
    finished_sandwich.append(cur_sandwich)
print (finished_sandwich)
6
  • Please format your question properly. Indentation is critical in Python. Without a proper layout your code could be misinterpreted Commented Feb 19, 2022 at 8:58
  • 1
    You need to reinitialise sandwich_orders before your for loop. The list is empty after your while loop so the for loop never runs Commented Feb 19, 2022 at 9:07
  • sandwich_orders.pop() is emptying the sandwich_orders list Commented Feb 19, 2022 at 9:09
  • 1
    Even if the sandwich_orders list was populated prior to the for loop, the code makes no sense. What are you trying to achieve with the for loop? Commented Feb 19, 2022 at 9:12
  • 1
    Never add or remove elements from a list while you're iterating over it with a for loop. Commented Feb 19, 2022 at 13:57

2 Answers 2

1

The for loop could be written thus:

sandwich_orders = ['chicken sandwich', 'beef sandwich', 'avocado sandwich', 'pork sandwich']
finished_sandwich = []

for _ in sandwich_orders[:]:
  finished_sandwich.append(sandwich_orders.pop())

...or, to retain the order of the original list:

for _ in sandwich_orders[:]:
      finished_sandwich.insert(0, sandwich_orders.pop())

...or, to avoid loops altogether:

finished_sandwich = sandwich_orders[:]
Sign up to request clarification or add additional context in comments.

5 Comments

you need to explain what : sandwich_orders[:] is
@pippo1980 Why do I need to explain that? It's been an inherent part of the language since Python2
cause I am a newbie like ullie and didnt see the finished_sandwich= [] mising in SAI answer and I need that to upvote your answer and there is no need for a deepcopy
Okey: if a is a list, a[:] returns a new object that is a copy of a:
if a is a list, a[:] returns a new object that is a copy of a: ``` >>> a = ['foo', 'bar', 'baz', 'qux', 'quux', 'corge'] >>> a[:] ['foo', 'bar', 'baz', 'qux', 'quux', 'corge'] >>> a[:] is a False ``` While If s is a string, s[:] returns a reference to the same object: ``` >>> s = 'foobar' >>> s[:] 'foobar' >>> s[:] is s True ``` Taken from [realpython.com/python-lists-tuples
0

After while loop, your variable sandwich_orders becomes empty as you are popping elements from it during the while loop. So you need to reinitialize it.Try in this way

sandwich_orders = ['chicken sandwich', 'beef sandwich', 'avocado sandwich', 'pork sandwich']
finished_sandwich = []

while sandwich_orders:
    cur_sandwich = sandwich_orders.pop()
    finished_sandwich.append(cur_sandwich)
print (finished_sandwich)

sandwich_orders = ['chicken sandwich', 'beef sandwich', 'avocado sandwich', 'pork sandwich']    
finished_sandwich= []

for cur_sandwich in sandwich_orders:
    finished_sandwich.append(cur_sandwich)
print (finished_sandwich)

5 Comments

But OP wants to pop() elements from sandwich_orders. Your for loop doesn't affect that list
I assumed he want to append elements from sandwich_orders to finished_sandwich using while and for loops.
I tried using pop for the for loop. But is popping every alternate element from the list
loop over a deepcopy of sandwich_orders (import copy, a = copy.deepcopy(sandwich_orders)) and pop from sandwich_orders
@pippo1980 There is no need for a deep copy

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.