
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Count of elements matching particular condition in Python
In this article we will see how to get some selected elements out of a Python list. So we need to design some condition and only the elements satisfying that condition should be picked and their count to be printed.
Wit in and sum
In this approach we use in condition to pick the elements and use some to get their count. 1 is used if the element is present else 0 is used for the result of in condition.
Example
Alist = ['Mon', 'Wed', 'Mon', 'Tue', 'Thu'] # Given list print("Given list:\n", Alist) cnt = sum(1 for i in Alist if i in('Mon','Wed')) print("Number of times the condition is satisfied in the list:\n",cnt)
Output
Running the above code gives us the following result −
Given list: ['Mon', 'Wed', 'Mon', 'Tue', 'Thu'] Number of times the condition is satisfied in the list: 3
With map and lambda
Here also used in condition but also use lambda and map functions. Final we apply the sum function to get the count.
Example
Alist = ['Mon', 'Wed', 'Mon', 'Tue', 'Thu'] # Given list print("Given list:\n", Alist) cnt=sum(map(lambda i: i in('Mon','Wed'), Alist)) print("Number of times the condition is satisfied in the list:\n",cnt)
Output
Running the above code gives us the following result −
Given list: ['Mon', 'Wed', 'Mon', 'Tue', 'Thu'] Number of times the condition is satisfied in the list: 3
With reduce
The reduce function applies a particular function to all the elements in a list supplied to it as an argument. We use it along with a in condition finally producing the count of the elements matching the condition.
Example
from functools import reduce Alist = ['Mon', 'Wed', 'Mon', 'Tue', 'Thu'] # Given list print("Given list:\n", Alist) cnt = reduce(lambda count, i: count + (i in('Mon','Wed')), Alist, 0) print("Number of times the condition is satisfied in the list:\n",cnt)
Output
Running the above code gives us the following result −
Given list: ['Mon', 'Wed', 'Mon', 'Tue', 'Thu'] Number of times the condition is satisfied in the list: 3
- Related Articles
- Python – Sort by a particular digit count in elements
- Count the Number of matching characters in a pair of string in Python
- Program to count items matching a rule using Python
- Count Frequency of Highest Frequent Elements in Python
- Count elements in a vector that match a target value or condition in C++
- Update an array element matching a condition using $push in MongoDB
- Python – Consecutive identical elements count
- Count frequencies of all elements in array in Python\n
- Wildcard Matching in Python
- Count distinct elements in an array in Python
- Find all elements count in list in Python
- Count the Number of matching characters in a pair of Java string
- Count the elements till first tuple in Python
- Count occurrence of all elements of list in a tuple in Python
- Matching Versus Searching in Python
