• 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
  • VBA
  • About
You are here: Home / Python / Using Python to Count Items in List Matching Criteria

Using Python to Count Items in List Matching Criteria

May 9, 2022 Leave a Comment

In Python, to count the items in a list which match a certain criterion, you can use comprehension and the Python sum() function.

lst = [5, 6, 2, 9, -1, 3]

count_gt_4 = sum(x > 4 for x in lst)

print(count_gt_4)

#Output:
3

You can also use an if statement to count items in a list matching a certain criterion with comprehension as shown below.

lst = [5, 6, 2, 9, -1, 3]

count_gt_4 = len([x for x in lst if x > 4])

print(count_gt_4)

#Output:
3

When working with collections of data in Python, the ability to easily get statistics and additional information about your data is very valuable.

One such piece of information when working with lists in Python is the count of items which match a given criteria.

With comprehension, we can easily get the items matching given criteria and then use the Python sum() function to get the count of the list.

Below is an example showing how you can get the count of items in a list matching criteria with Python. Comprehension here returns a list of boolean values and sum() sums up the number of ‘True’ items.

lst = [5, 6, 2, 9, -1, 3]

count_gt_4 = sum(x > 4 for x in lst)

print(count_gt_4)

#Output:
3

Using List Comprehension to Get Count of Items Matching Criteria in Python

If you have conditions which are a little more complex or have slightly different requirements, you can use comprehension with the len() function to get the count of items matching a particular criteria.

In this example, you first build the list and then find the length of the filtered list.

Depending on your code, this may be better than the previous example.

Below is an example of how you can use an if statement with list comprehension to filter a list in Python, and then get the length of that list.

lst = [5, 6, 2, 9, -1, 3]

filtered_lst = [x for x in lst if x > 4]

count_gt_4 = len(filtered_lst)

print(filtered_lst)
print(count_gt_4)

#Output:
[5, 6, 9]
3

This can be useful if you want to use the filtered list later in your code.

Hopefully this article has been useful for you to learn how to count the number of items in a list which match criteria in Python.

Other Articles You'll Also Like:

  • 1.  math.radians() python – How to Convert Degrees to Radians in Python
  • 2.  Remove Empty Strings from List in Python
  • 3.  Find Index of Maximum in List Using Python
  • 4.  Intersection of Two Lists in Python
  • 5.  Python Indicator Function – Apply Indicator Function to List of Numbers
  • 6.  How to Group and Aggregate By Multiple Columns in Pandas
  • 7.  Length of Set Python – Get Set Length with Python len() Function
  • 8.  How to Serialize a Model Object with a List of Lists Using Django Rest Framework in Python
  • 9.  Drop Duplicates pandas – Remove Duplicate Rows in DataFrame
  • 10.  Adjusting Python Turtle Screen Size with screensize() Function

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

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

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 © 2022 · The Programming Expert · About · Privacy Policy