• 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 Count Number of True in List

Using Python to Count Number of True in List

July 6, 2022 Leave a Comment

To count the number of True values in a list in Python, the easiest way is with list comprehension and the Python len() function.

lst = [True, False, True, False]

count = len([val for val in lst if val == True])

print(count)

#Output:
2

If you have a list which has numbers in it, you will have to be careful since True is equal to 1. In this case, you should also check if the variable type is a bool.

lst = [True, False, True, False, 1, 2, 3]

count = len([val for val in lst if val == True and type(val) == type(True)])

print(count)

#Output:
2

You can also use sum() if your list only has boolean values.

lst = [True, False, True, False]

print(sum(lst))

#Output:
2

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 number of True values in a list.

To count the True values in a list in Python, the easiest way is with list comprehension and the Python len() function. You can use an if statement to get if the value is True or not.

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

lst = [True, False, True, False]

count = len([val for val in lst if val == True])

print(count)

#Output:
2

If you have a list which has numbers in it, you will have to be careful since True is equal to 1. In this case, you should also check if the variable type is a bool.

lst = [True, False, True, False, 1, 2, 3]

count = len([val for val in lst if val == True and type(val) == type(True)])

print(count)

#Output:
2

Finding Count of True Values with sum() in Python

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

The Python sum() function is very useful as well when summarizing data.

You can use sum() if your list only has boolean values to get the count of Trues since True is equal to 1, and False is equal to 0.

Below is an example showing you how to use sum() to get the count of True values in a list using Python.

lst = [True, False, True, False]

print(sum(lst))

#Output:
2

Get Count of Number of False in List Using Python

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

All you need to do is change the if statement.

Below is an example showing you how to count the number of False values in a list using Python.

lst = [True, False, True, False]

count = len([val for val in lst if val == False])

print(count)

#Output:
2

Hopefully this article has been useful for you to learn how to count the number of True values in a list using Python.

Other Articles You'll Also Like:

  • 1.  Create Unique List from List in Python
  • 2.  Get Last Day of Month Using Python
  • 3.  Python Get Yesterday’s Date
  • 4.  Using Python to Check if Queue is Empty
  • 5.  Using Python to Remove Quotes from String
  • 6.  pandas cumsum – Find Cumulative Sum of Series or DataFrame
  • 7.  Get First Day of Month Using Python
  • 8.  Truncate Decimal from Number with Python math.trunc() Function
  • 9.  Check if Variable is Tuple in Python
  • 10.  Find Median of List in Python

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