Python Forum
understanding basic loop behaviour
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
understanding basic loop behaviour
#1
Hello,

I'm finding it difficult to understand the behaviour of the while loop inside the for loop. To be more exact, I don't understand why the output.append function is able to gather all the words, even if it's inside the for loop. So here is the code:
state_names = ["Alabama", "California", "Oklahoma", "Florida"]
vowels = list('aeiou')
output = []

for state in state_names:
    state_list = list(state.lower())

    for vowel in vowels:
        while True:
            try:
                state_list.remove(vowel)
            except:
                break
    output.append(''.join(state_list).capitalize())
print(output)
So I would basically except output.append to work just like it would work if it were outside the for loop, that is to say, the to output only the last word without the vowels:
['Flrd']
But somehow all the words are added, and I'm not sure how this is happening, if there is no statement inside the while loop. Can anyone take this apart for me?

Thank you!
Reply
#2
There is code inside the while loop...

try:
    state_list.remove(vowel)
except:
    break
Put a break point on it and you will see.

[Image: python-1.png]
There is no passion to be found playing small - in settling for a life that is less than the one you are capable of living.
Reply
#3
Maybe I should have mentioned from the very beginning that I'm really not a knowledgeable programmer (I mean, I'm not a programmer at all), but I thought the question that I've asked would already reveal that.
So what I mean to say is that I've no idea what a breakpoint is.

What I have done, though, is place the output.append insode the while loop and then I saw that it added all the intermediate steps such as "labama", "lbama" and so on. But I still don't really understand why all the information is kept also outside the while loop, but not outside the for loop.
Reply
#4
In line 5 you start the loop which will list each state.
Line 14 is inside that loop so it is done for each state.
Reply
#5
check out our loop tutorial for the basic concepts.

The outer for loop is looping each state, the inner for loop is looping each vowel in each state. The while loop is removing the vowel. Then each string is joined together and appended back into a new list output.

Quote:
(Feb-10-2020, 05:48 PM)vinci Wrote: So I would basically except output.append to work just like it would work if it were outside the for loop, that is to say, the to output only the last word without the vowels:
output is being appended to each loop in the outer loop. If it was outside the loop, then nothing would be in it by the end. You can just splice the list to get the last element
print(output[-1])
There are other ways to remove vowels.

>>> state_names = ["Alabama", "California", "Oklahoma", "Florida"]
>>> for state in state_names:
...     table = state.maketrans(dict.fromkeys('aeiouAEIOU'))
...     new = state.translate(table)
...     print(new.capitalize())
... 
Lbm
Clfrn
Klhm
Flrd
Recommended Tutorials:
Reply
#6
Ok, I've changed two code to something like this in order to make more sense of what is actually happening, and now I have a better understanding of what's going on, even though I still find it somewhat hard to ingest it:
state_names = ["Alabama", "California", "Oklahoma", "Florida"]
vowels = list('aeiou')
output = []

for state in state_names:
    state_list = list(state.lower())
    print("This is the state_list {}.".format(state_list))

    for vowel in vowels:
        while True:
            try:
                state_list.remove(vowel)
                print("This is output inside the while loop: {}".format(state_list))
            except:
                break
        print("This is the output inside the inner for loop: {}".format(state_list))
    output.append(''.join(state_list).capitalize())
    print("This is the output inside the main for loop: {}".format(output))

print("This is the output completely outside the loops: {}".format(state_list))
Thank you for the answers.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Basic binary search algorithm - using a while loop Drone4four 1 309 Jan-22-2024, 06:34 PM
Last Post: deanhystad
  logger behaviour setdetnet 1 853 Apr-15-2023, 05:20 AM
Last Post: Gribouillis
  can someone explain this __del__ behaviour? rjdegraff42 1 692 Apr-12-2023, 03:25 PM
Last Post: deanhystad
  Asyncio weird behaviour vugz 2 1,191 Apr-09-2023, 01:48 AM
Last Post: vugz
  Weird behaviour using if statement in python 3.10.8 mikepy 23 3,430 Jan-18-2023, 04:51 PM
Last Post: mikepy
  Generator behaviour bla123bla 2 1,072 Jul-26-2022, 07:30 PM
Last Post: bla123bla
  Inconsistent behaviour in output - web scraping Steve 6 2,446 Sep-20-2021, 01:54 AM
Last Post: Larz60+
  Adding to the dictionary inside the for-loop - weird behaviour InputOutput007 5 2,651 Jan-21-2021, 02:21 PM
Last Post: InputOutput007
  Behaviour of 2D array SimonB 6 2,761 Jan-21-2021, 01:29 PM
Last Post: SimonB
  strange behaviour- plotting nathan_Blanc_Haifa 0 1,469 Dec-27-2020, 01:37 PM
Last Post: nathan_Blanc_Haifa

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020