• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Using Python to Sum Odd Numbers in List

Using Python to Sum Odd Numbers in List

July 3, 2022 Leave a Comment

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

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

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

print(s)

#Output:
13

You can also use a loop to sum the number of odd numbers in a list in Python.

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

def sumOdds(l):
    sum = 0
    for num in l:
        if num % 2 != 0:
            sum = sum + num
    return sum

print(sumEvens(lst))

#Output:
13

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 sum the odd numbers in a list.

To sum the odd numbers in a list in Python, the easiest way is with list comprehension and the Python sum() 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 sum the number of odd numbers in 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

Finding Count of Odd Numbers Using len() in Python

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

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

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

Below is an example showing you how to count the odd numbers of 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

Get Sum of Even Numbers in List Using Python

If you want to go the other way and get the sum 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 odd or odd, for odd numbers we want equality.

Below is an example showing you how to sum the even numbers in 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:
12

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

Other Articles You'll Also Like:

  • 1.  List of Turtle Shapes in Python
  • 2.  Python Replace Space with Underscore Using String replace() Function
  • 3.  Count Number of Files in Directory with Python
  • 4.  Using pandas to_csv() Function to Append to Existing CSV File
  • 5.  Using Python to Print Plus or Minus Sign Symbol
  • 6.  Selenium maximize_window() Function to Maximize Window in Python
  • 7.  Get Elapsed Time in Seconds in Python
  • 8.  Python Check if Object Has Attribute
  • 9.  Using Python to Remove Duplicates from List
  • 10.  Using Python to Split String by Tab

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy