- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Count positive and negative numbers in a list in Python program

In this article, we will learn about the solution to the problem statement given below.

**Problem statement **− We are given a list iterable, we need to count positive and negative numbers in it and display them.

## Approach 1 − Brute-force approach using iteration construct(for)

Here we need to iterate each element in the list using a for loop and check whether num>=0, to filter the positive numbers. If the condition evaluates to be true, then increase pos_count otherwise, increase neg_count.

### Example

list1 = [1,-2,-4,6,7,-23,45,-0] pos_count, neg_count = 0, 0 # enhanced for loop for num in list1: # check for being positive if num >= 0: pos_count += 1 else: neg_count += 1 print("Positive numbers in the list: ", pos_count) print("Negative numbers in the list: ", neg_count)

### Output

Positive numbers in the list: 5 Negative numbers in the list: 3

## Approach 2 − Brute-force approach using iteration construct(while)

Here we need to iterate each element in the list using a for loop and check whether num>= 0, to filter the positive numbers. If the condition evaluates to be true, then increase pos_count otherwise, increase neg_count.

### Example

list1 = [1,-2,-4,6,7,-23,45,-0] pos_count, neg_count = 0, 0 num = 0 # while loop while(num < len(list1)): # check if list1[num] >= 0: pos_count += 1 else: neg_count += 1 # increment num num += 1 print("Positive numbers in the list: ", pos_count) print("Negative numbers in the list: ", neg_count)

### Output

Positive numbers in the list: 5 Negative numbers in the list: 3

## Approach 3 − Using Python Lambda Expressions

Here we take the help of filter and lambda expressions we can directly differentiate between positive and negative numbers.

### Example

list1 = [1,-2,-4,6,7,-23,45,-0] neg_count = len(list(filter(lambda x: (x < 0), list1))) pos_count = len(list(filter(lambda x: (x >= 0), list1))) print("Positive numbers in the list: ", pos_count) print("Negative numbers in the list: ", neg_count)

### Output

Positive numbers in the list: 5 Negative numbers in the list: 3

All the variables are declared in the local scope and their references are seen in the figure above.

## Conclusion

In this article, we have learned how to count positive and negative numbers in a list.

- Related Articles
- Python program to count positive and negative numbers in a list
- Lambda expression in Python Program to rearrange positive and negative numbers
- Golang Program to Print the Sum of all the Positive Numbers and Negative Numbers in a List
- Python program to print negative numbers in a list
- Lambda expression in Python to rearrange positive and negative numbers
- Python program to Count Even and Odd numbers in a List
- Reversing negative and positive numbers in JavaScript
- Distinguish positive and negative numbers.
- Java Program to convert positive int to negative and negative to positive
- Explain the difference between positive and negative numbers.
- Count Negative Numbers in a Column-Wise and Row-Wise Sorted Matrix using Python?
- Rearrange positive and negative numbers using inbuilt sort function in C++
- Rearrange positive and negative numbers with constant extra space in C++
- Implement Bubble sort with negative and positive numbers – JavaScript?
- Write a program in Python to shift a dataframe index by two periods in positive and negative direction