- Related Questions & Answers
- Python program to count positive and negative numbers in a list
- Lambda expression in Python Program to rearrange positive and negative numbers
- Lambda expression in Python to rearrange positive and negative numbers
- Python program to print negative numbers in a list
- Java Program to convert positive int to negative and negative to positive
- Sum a negative number (negative and positive digits) - JavaScript
- Python program to Count Even and Odd numbers in a List
- Implement Bubble sort with negative and positive numbers – JavaScript?
- Count Negative Numbers in a Column-Wise and Row-Wise Sorted Matrix using Python?
- Split an array of numbers and push positive numbers to JavaScript array and negative numbers to another?
- Counting number of positive and negative votes in MySQL?
- How to print positive and negative infinity values in JavaScript?
- What are the positive and negative impacts of a working mother?
- Check if array elements are consecutive in O(n) time and O(1) space (Handles Both Positive and negative numbers) in Python
- Program to check if a number is Positive, Negative, Odd, Even, Zero?

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

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.

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.

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)

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

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.

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)

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

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

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)

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.

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

Advertisements