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)
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
  • Please format your question properly. Indentation is critical in Python. Without a proper layout your code could be misinterpreted – DarkKnight Feb 19 '22 at 08: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 – jezza_99 Feb 19 '22 at 09:07
  • sandwich_orders.pop() is emptying the sandwich_orders list – pippo1980 Feb 19 '22 at 09: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? – DarkKnight Feb 19 '22 at 09:12
  • check here an interesting question on while loops and lists https://stackoverflow.com/questions/52876593/why-does-an-empty-list-evaluates-to-false-on-a-while-loop-in-python – pippo1980 Feb 19 '22 at 09:21
  • 1
    Never add or remove elements from a list while you're iterating over it with a `for` loop. – CrazyChucky Feb 19 '22 at 13:57

2 Answers2

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[:]
DarkKnight
  • 19,739
  • 3
  • 6
  • 22
  • you need to explain what : sandwich_orders[:] is – pippo1980 Feb 19 '22 at 09:54
  • 1
    @pippo1980 Why do I need to explain that? It's been an inherent part of the language since Python2 – DarkKnight Feb 19 '22 at 09:55
  • 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 – pippo1980 Feb 19 '22 at 10:04
  • Okey: if a is a list, a[:] returns a new object that is a copy of a: – pippo1980 Feb 19 '22 at 15:40
  • 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 [https://realpython.com/python-lists-tuples/ – pippo1980 Feb 21 '22 at 23:12
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)
pippo1980
  • 2,181
  • 3
  • 14
  • 30
SAI SANTOSH CHIRAG
  • 2,046
  • 1
  • 10
  • 26