To count the odd numbers in a list in Python, the easiest way is with list comprehension and the Python len() function.

lst = [0, 4, 6, 9, 2, 3, 1]

count = len([num for num in lst if num % 2 != 0])

print(count)

#Output:
3

You can also use a loop to count the number of even numbers in a list in Python.

lst = [0, 4, 6, 9, 2, 3, 1]

def countOdds(l):
    count = 0
    for num in l:
        if num % 2 != 0:
            count = count + 1
    return count

print(countOdds(lst))

#Output:
3

When working with collections of data, the ability to easily summarize and get statistics about the collection is valuable.

One such case is if you want to count the odd numbers in a list.

To count the odd numbers in a list in Python, the easiest way is with list comprehension and the Python len() function. To get the odd numbers, we just need to check if the number is even or odd.

Below is a simple example showing you how to count the number of odd numbers in a list using Python.

lst = [0, 4, 6, 9, 2, 3, 1]

count = len([num for num in lst if num % 2 != 0])

print(count)

#Output:
3

Finding Sum of Odd Numbers Using sum() in Python

You can use other functions to summarize collections of data in Python just like with length.

One example is if you want to find the sum of the odd numbers of a list.

In this case, you can use the Python sum() function.

Below is an example showing you how to sum the odd numbers of a list using Python.

lst = [0, 4, 6, 9, 2, 3, 1]

s = sum([num for num in lst if num % 2 != 0])

print(s)

#Output:
13

Get Count of Even Numbers in List Using Python

If you want to go the other way and get the count of even numbers in a list using Python, you can just make a simple adjustment to the code above.

When using % to check if the number is even or odd, for even numbers we want equality.

Below is an example showing you how to count the even numbers in a list using Python.

lst = [0, 4, 6, 9, 2, 3, 1]

count = len([num for num in lst if num % 2 == 0])

print(count)

#Output:
4

Hopefully this article has been useful for you to learn how to count the odd numbers in a list using Python.

Categorized in:

Python,

Last Update: February 26, 2024