You're returning immediately in both the if and else blocks. That ends the loop in both cases.

You should only return in the if block. If you make it through the entire loop without returning, you know that all the words are title case.

def all_title_case(word_list): 
    for word in word_list: 
        if not word.istitle():
            return False
    return True 

You can also use the all() function instead of a loop.

def all_title_case(word_list): 
    return all(word.istitle() for word in word_list)
Answer from Barmar on Stack Overflow
Top answer
1 of 1
3

Many iterable objects in Python - things that you could put after in in a for loop - can only be iterated over once. After that, they're done; they can't go back to the beginning, and any further attempts to iterate over them will act as if they contain nothing. A csv.reader object is one example of this: in the first iteration of your outer loop, you iterate through all the available records that matrix_reader can provide. That's why, the next time the code comes around to that line, it looks as if matrix_reader is empty.

Perhaps the easiest way to solve this is to make a new matrix_reader each time you want to iterate over it. Like so:

for line in csv_reader:
    matrix_reader = ...
    for mline in matrix_reader:
        ...

To understand why csv.reader gets exhausted after you go through it once, you should know that a csv.reader does not represent a CSV file. Actually, despite the name, it's really more of a "converter": it takes lines of text from some source, which could be anything, and converts them into lists, one by one. After the reader has converted a line, it forgets about it. This allows the reader object to process millions of lines without taking a huge chunk of memory.

The tradeoff of this approach is that the reader object can't go back to lines it has processed before unless it can somehow tell its source of text to go back and repeat a previous line. But there's no guarantee that the underlying source can do that. If the source is the output from some other program, for example, you can't tell the program to go back and repeat an old line of output. Or if the source is text being streamed over the internet, you can't necessarily tell it to repeat a line that had been streamed before. So the reader can't count on being able to access old lines, and that's why, when it's gotten to the last one, the only reasonable behavior is for it to act as if it has nothing left.

Discussions

First "for loop" stops after first iteration python - Stack Overflow
I am reading two different files with for loop. First "for loop" stops after first iteration. The print output is only line1 of f1 with all lines of f2 but then exit the loop. for line1 in f1: More on stackoverflow.com
🌐 stackoverflow.com
Python for loop stops working after first iteration - Stack Overflow
I have a for loop that returns zeros after the first iteration. I have it print out i, so I know it is actually looping through, but for some reason it doesn't seem to be calling my function, new_l... More on stackoverflow.com
🌐 stackoverflow.com
April 21, 2017
For Loop stops after first iteration
Hi New here 🙂. I am trying to write a function to find the Smallest Common Multiple between a range of 2 numbers in an array. The multiple has to be divisible by all numbers in the range. I approached the problem as follows: step 1: Create a new array and push all the numbers in range between ... More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
11
0
October 12, 2022
python - How do I stop this from exiting after the first iteration? - Stack Overflow
I'm trying to return a statement ... but the loop keeps exiting after the first iteration. def test(s, old_ch, new_ch): """returns the given input and replaces an old character with a new character""" newstring = "" for ch in s: while (ch == old_ch): newstring += new_ch break while (ch != old_ch): newstring += ch break return newstring · I know that there are already defined replace functions in python, but this ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Reddit
reddit.com › r/learnpython › loop stops after first iteration (only sends one api request). however, when simply printing the contents i'm trying to send, it iterates completely. more inside.
r/learnpython on Reddit: Loop stops after first iteration (only sends one API request). However, when simply printing the contents I'm trying to send, it iterates completely. More inside.
December 10, 2020 -

As the title implies, I'm attempting to use a for loop to send multiple requests to an API. When running the code, it only sends the first request. However, when updating the for loop to simply print the data in the multiple requests I'm sending, it prints all of the expected data, iterating without issue. Anyone know what's going on here?

This will iterate completely

def stuff():
    for i in thing():
        print(i)

When using the API information applied to the for loop, it only sends the first iterable.

def stuff():
    for i in thing():
        #api stuff
        payload['fields']['name'] = i[0]
        payload['fields']['date'] = i[1]

        url = 'https://stuff.com'
        response = requests.post(url, data = json.dumps(payload), headers = HEADERS)
        result = json.loads(response.text)

        if response.status_code == 200:
            result = {'result' : 'progressing', 'message' : 'Task Scheduled.'}

        return {
            'statusCode': response.status_code,
            'headers': { 'Content-Type': 'application/json' },
            'body': json.dumps(result)
        }
🌐
Codecademy
codecademy.com › forum_questions › 53516d00548c351007000bef
Why is this code not working ? Why does It stops looping ? | Codecademy
For a loop iteration to run it ONLY tests for the existence of a letter at the index that matches the internal loop count. If a letter exists there, it runs the code block for the loop.
🌐
Python Forum
python-forum.io › thread-17943.html
First for loop stops after first iteration
I am creating a matrix array by reading two different lists with for loop. But the second for loop stops after the first iteration. The print output is only with one parameter *1 instead of the all other that are in the lists in the list f1. impor...
🌐
freeCodeCamp
forum.freecodecamp.org › curriculum help
For Loop stops after first iteration - Curriculum Help - The freeCodeCamp Forum
October 12, 2022 - Hi New here 🙂. I am trying to write a function to find the Smallest Common Multiple between a range of 2 numbers in an array. The multiple has to be divisible by all numbers in the range. I approached the problem as follows: step 1: Create a new array and push all the numbers in range between the 2 numbers in the original array to this new array. step 2: Define an upper bound for the multiples. step 3: Loop through the multiples of the largest number, capping at the u...
Find elsewhere
🌐
Stack Exchange
gis.stackexchange.com › questions › 281565 › python-loop-stops-before-running-all-files
Python loop stops before running all files - Geographic Information Systems Stack Exchange
I actually think it was just an indentation issue with your FOR loop - as it is presented here, it iterates through all the LAS files, and only the last one gets the two commands added. Indent the lines as shown below.
🌐
Stack Overflow
stackoverflow.com › questions › 48549974 › why-python-for-loop-stops-at-the-first-instance-of-iteration
lambda - Why python for loop stops at the first instance of iteration? - Stack Overflow
February 1, 2018 - I don't see anything obviously wrong. I'm going to guess you either have your return indented wrong in your actual code, putting it inside the loop, or the list actually only has 1 element.
🌐
Stack Overflow
stackoverflow.com › questions › 20523455 › for-loop-in-python-stopping-before-i-want-it-to
For loop in Python stopping before I want it to - Stack Overflow
You can confirm this by print temp_str after the loop. So, you have to have this line at the end of the loop, to make sure that we gather the remaining item in temp_str ... Join us for our first community-wide AMA (Ask Me Anything) with Stack...
🌐
Stack Overflow
stackoverflow.com › questions › 51655172 › why-does-my-loop-stop-after-one-iteration
python - Why does my loop stop after one iteration? - Stack Overflow
using the generator, you won't have the list of all the lines, but an "iterable", you could call it "lazy-reader", in order to use it you have to iterate over it · >>> for line in get_lines(".\\x"): ...
🌐
Stack Overflow
stackoverflow.com › questions › 46251158 › python-for-loops-stops-after-first-iteration
Python for loops stops after first iteration - Stack Overflow
I’m having trouble debugging my get_secondary_connections function below. For some reason, my for friend in network[user][‘connections’] loop always stops at the first value in the list instead of
🌐
DevDreamz
devdreamz.com › question › 901058-for-loop-stops-after-first-iteration-python
for loop stops after first iteration - python - DevDreamz
Many iterable objects in Python - things that you could put after in in a for loop - can only be iterated over once. After that, they're done; they can't go back to the beginning, and any further attempts to iterate over them will act as if ...