'how to find the length of each element in a list python? then i need to calculate how many of them have more than 5 words

def counts(lst):
    count = 0
    for elem in lst:
        if len(elem) > 5:
            count += 1
    return count


lst = []
num = int(input("How Many Elements You Need To Add: "))
for i in range(num):
    ele = input()
    lst.append(ele)

print(counts(lst))


Solution 1:[1]

x, y = {i: len(i) for i in my_list}, len([i for i in my_list if len(i) > 5])
print('x: ', x)
print('y: ', y)

Output:

x:  {'Tfjklfq': 7, 'dfdf': 4, 'd': 1, 'fdkmqdjfksd': 11}
y:  2

Solution 2:[2]

By the looks of it , You already have the right code, Although I could not find any issue in your code as it is not formatted.

Below is a working example ( just indented code a bit )

def counts(lst): 
    count = 0 
    for elem in lst: 
        if len(elem) > 5: 
            count += 1 
    return count

lst = [] 
num = int(input("How Many Elements You Need To Add: ")) 
for i in range(num): 
    ele = input(f'Enter Element {i+1}:') 
    lst.append(ele)

print(f"total elements greater than five : {counts(lst)}")

You can run the code online here

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 namos
Solution 2