Looping over a list - istitle() method


I would like to create a function, checking if every string in a list is in titlecase, returning True if this is the case, False if not

I presume that I would need to loop over the list with a for loop, and then either using an if else statement or a while loop - what would you recommend doing here and is what I am doing here completely off?

eg list
list1 = [“ABC”, “Abc”, “Abc”]


This is a good place to use a comprehension and all().

all(x.istitle() for x in list1)

I completely agree with BowlOfRed’s suggestion to use all() and a
comprehension, except I would use a more meaningful variable name, like
“s” (for string) or “string”, rather than “x” which is normally assumed
to be some kind of number.

But for learning purposes, we can write this:

flag = True
for s in list_of_strings:
    if not s.istitle():
        flag = False
        break  # No need to keep scanning.

to check each string. If you find a string that is not titlecase, you
can bail out: it only takes one non-titlecase string for the list to
not be all titlecase.