'There has to be a better way to do this

The following was approved in my class, but it looks so bulky. Would there be a better way to write this?

def tel_kleine_letters(string):
    aantal_kleine_letters = 0
    for karakter in string:
        if 'a' <= karakter <= 'z': # letter is kleine letter
            aantal_kleine_letters += 1
    return aantal_kleine_letters
    lijst = ["Een", "Twee", "Drie"]

    for woord in lijst:
        aantal = tel_kleine_letters(woord)
        print("er zitten", aantal, "klene letters in", word)

    totaal_kleine_letters = 0
    for woord in lijst:
        for letter in woord:
            if 'a' <= letter <= 'z': # letter is kleine letter
                totaal_kleine_letters += 1

    print("er zitten totaal", totaal_kleine_letters, "kleine letters in", lijst)


Solution 1:[1]

tel_kleine_letters could be written more simply by taking advantage of str.islower() and sum:

def tel_kleine_letters(string):
    return sum(c.islower() for c in string)

Also, you're re-implementing your entire tel_kleine_letters function to recalculate the number that you've already gotten in the earlier loop. You could just keep track of the total in the loop where you print all the individual words:

woords = ["Een", "Twee", "Drie"]
totaal = 0

for w in woords:
    k = tel_kleine_letters(w)
    print(f"er zitten {k} kleine letters in {w}")
    totaal += k

print(f"er zitten totaal {totaal} kleine letters in {woords}")

If you needed to compute the total independently of that loop, you could use the sum of tel_kleine_letters for each string in the list, similar to how we used it earlier to sum all the lowercase letters in a single string:

totaal = sum(tel_kleine_letters(w) for w in woords)
print(f"er zitten totaal {totaal} kleine letters in {woords}")

Another option would be to join all the strings together and then use your function to count the lowercase letters in the joined string:

totaal = tel_kleine_letters(''.join(woords))
print(f"er zitten totaal {totaal} kleine letters in {woords}")

Solution 2:[2]

As @Code-Apprentice also pointed out, you can add aantal to totaal_kleine_letters instead of doing all of the work again. It would then look something like this:

def tel_kleine_letters(string):
    aantal_kleine_letters = 0
    for karakter in string:
        if 'a' <= karakter <= 'z': # letter is kleine letter
            aantal_kleine_letters += 1
    return aantal_kleine_letters

lijst = ["Een", "Twee", "Drie"]

totaal_kleine_letters = 0
for woord in lijst:
    aantal = tel_kleine_letters(woord)
    print("er zitten", aantal, "klene letters in", word)
    totaal_kleine_letters += aantal

print("er zitten totaal", totaal_kleine_letters, "kleine letters in", lijst)

You can also try using the python builtin function filter, map and a lambda expression. It will probably be less performant but for a small amount of characters it will be fine.

def tel_kleine_letters(string):
    return sum(
        map(
            lambda word: len(list(filter(lambda c: "a" <= c <= "z", word))),
            ["Een", "Twee", "Drie"],
        )
    )

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Wouterr