Related Articles

# Python | Count of elements matching particular condition

• Last Updated : 30 Dec, 2018

Checking a number/element by a condition is a common problem one faces and is done in almost every program. Sometimes we also require to get the totals that match the particular condition to have a distinguish which to not match for further utilization. Lets discuss certain ways in which this task can be achieved.

Method #1 : Using `sum()` + generator expression
This method uses the trick of adding 1 to the sum whenever the generator expression returns true. By the time list gets exhausted, summation of count of numbers matching a condition is returned.

 `# Python 3 code to demonstrate ``# to get count of elements matching condition ``# using sum() + generator expression`` ` `# initializing list``test_list ``=` `[``3``, ``5``, ``1``, ``6``, ``7``, ``9``]`` ` `# printing original list``print` `(``"The original list is : "` `+` `str``(test_list))`` ` `# using sum() + generator expression``# to get count of elements matching condition ``# checks for odd``res ``=` `sum``(``1` `for` `i ``in` `test_list ``if` `i ``%` `2` `!``=` `0``)`` ` `# printing result``print` `(``"The number of odd elements: "` `+` `str``(res))`

Output :

```The original list is : [3, 5, 1, 6, 7, 9]
The number of odd elements: 5
```

Method #2 : Using `sum()` + `map()``map()` does the task almost similar to the generator expression, difference is just the internal data structure employed by it is different hence more efficient.

 `# Python 3 code to demonstrate ``# to get count of elements matching condition ``# using sum()+ map()`` ` `# initializing list``test_list ``=` `[``3``, ``5``, ``1``, ``6``, ``7``, ``9``]`` ` `# printing original list``print` `(``"The original list is : "` `+` `str``(test_list))`` ` `# using sum()+ map()``# to get count of elements matching condition ``# checks for odd``res ``=` `sum``(``map``(``lambda` `i: i ``%` `2` `!``=` `0``, test_list))`` ` `# printing result``print` `(``"The number of odd elements: "` `+` `str``(res))`

Output :

```The original list is : [3, 5, 1, 6, 7, 9]
The number of odd elements: 5
```

Method #3 : Using `reduce() + lamda`
reduce function does the task of accumulation as the sum function in the above used methods. lambda is used to perform the condition against which result needs to be checked.

 `# Python 3 code to demonstrate ``# to get count of elements matching condition ``# using reduce() + lambda`` ` `# initializing list``test_list ``=` `[``3``, ``5``, ``1``, ``6``, ``7``, ``9``]`` ` `# printing original list``print` `(``"The original list is : "` `+` `str``(test_list))`` ` `# using reduce() + lambda``# to get count of elements matching condition ``# checks for odd``res ``=` `reduce``(``lambda` `count, i: count ``+` `(i ``%` `2` `!``=` `0``), test_list, ``0``)`` ` `# printing result``print` `(``"The number of odd elements: "` `+` `str``(res))`

Output :

```The original list is : [3, 5, 1, 6, 7, 9]
The number of odd elements: 5
```

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up