Given a list of numbers, write a Python program to count positive and negative numbers in a List.
Example:
Input: list1 = [2, -7, 5, -64, -14] Output: pos = 2, neg = 3 Input: list2 = [-12, 14, 95, 3] Output: pos = 3, neg = 1
Example #1: Count positive and negative numbers from given list using for loop
Iterate each element in the list using for loop and check if num >= 0, the condition to check positive numbers. If the condition satisfies, then increase pos_count else increase neg_count.
# Python program to count positive and negative numbers in a List # list of numbers list1 = [10, -21, 4, -45, 66, -93, 1] pos_count, neg_count = 0, 0 # iterating each number in list for num in list1: # checking condition 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: 4 Negative numbers in the list: 3
Example #2: Using while loop
# Python program to count positive and negative numbers in a List # list of numbers list1 = [-10, -21, -4, -45, -66, 93, 11] pos_count, neg_count = 0, 0num = 0 # using while loop while(num < len(list1)): # checking condition 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: 2 Negative numbers in the list: 5
Example #3 : Using Python Lambda Expressions
# Python program to count positive # and negative numbers in a List # list of numbers list1 = [10, -21, -4, 45, 66, 93, -11] neg_count = len(list(filter(lambda x: (x < 0), list1))) # we can also do len(list1) - neg_count 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: 4 Negative numbers in the list: 3
Example #4 : Using List Comprehension
# Python program to count positive # and negative numbers in a List # list of numbers list1 = [-10, -21, -4, -45, -66, -93, 11] only_pos = [num for num in list1 if num >= 1] pos_count = len(only_pos) print("Positive numbers in the list: ", pos_count) print("Negative numbers in the list: ", len(list1) - pos_count) |
Output:
Positive numbers in the list: 1 Negative numbers in the list: 6
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.
Recommended Posts:
- Python Program to find Sum of Negative, Positive Even and Positive Odd numbers in a List
- C program to count Positive and Negative numbers in an Array
- Find ratio of zeroes, positive numbers and negative numbers in the Array
- Python | Counting sign change in list containing Positive and Negative Integers
- Lambda expression in Python to rearrange positive and negative numbers
- Python Program to Check Whether a Number is Positive or Negative or zero
- Program to check if a number is Positive, Negative, Odd, Even, Zero
- C program to Check Whether a Number is Positive or Negative or Zero
- Python | Rearrange Positive and Negative Elements
- Python | Segregate Positive and Negative Integers from mixed string
- Highlight the negative values red and positive values black in Pandas Dataframe
- Python program to print negative numbers in a list
- Python program to print positive numbers in a list
- Python program to print all negative numbers in a range
- Count Negative Numbers in a Column-Wise and Row-Wise Sorted Matrix
- Python program to find Tuples with positive elements in List of tuples
- Python program to print all positive numbers in a range
- Python - Remove Negative Elements in List
- Python - Negative index of Element in List
- Python | Get positive elements from given list of lists
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

