Open In App

Python program to count positive and negative numbers in a list

Last Updated : 10 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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 the 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. 

Python3




# 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

Time Complexity: O(N), Here N is the number of items in the list.
Auxiliary Space: O(1), As constant extra space is used.

Example #2: Using while loop 

Python3




# 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, 0
num = 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

Time Complexity: O(N), Here N is the number of items in the list.
Auxiliary Space: O(1), As constant extra space is used.

Example #3: Using Python Lambda Expressions 

Python3




# 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

Time Complexity: O(N), Here N is the number of items in the list.
Auxiliary Space: O(1), As constant extra space is used.

Example #4: Using List Comprehension 

Python3




# 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

Time Complexity: O(N), Here N is the number of items in the list.
Auxiliary Space: O(1), As constant extra space is used.

Method: Using enumerate function 

Python3




l=[12, -7, 5, 64, -14];c=0
x=[a for j,a in enumerate(l) if a>=0]
print("Length of Positive numbers is:", len(x))
print("Length of Negative numbers is:", len(l)-len(x))


Output

Length of Positive numbers is: 3
Length of Negative numbers is: 2

Time Complexity: O(N), Here N is the number of items in the list.
Auxiliary Space: O(1), As constant extra space is used.

Method : Using startswith() method

Python3




# Python program to count positive and negative numbers in a List
 
# list of numbers
list1 = [10, -21, 4, -45, 66, -93, 1,0]
 
pos_count, neg_count = 0, 0
list2=list(map(str,list1))
 
# iterating each number in list
for num in list2:
    # checking condition
    if num.startswith("-"):
        neg_count += 1
    elif(num!="0"):
        if(not num.startswith("-")):
            pos_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

Time Complexity: O(N), Here N is the number of items in the list.
Auxiliary Space: O(1), As constant extra space is used.

Method: Using sum() method

Python3




l = [12, -7, 5, 64, -14];
x = sum(1 for i in l if i >= 0 )
print("Length of Positive numbers is:", x)
print("Length of Negative numbers is:", len(l)-x)


Output

Length of Positive numbers is: 3
Length of Negative numbers is: 2

Time Complexity: O(N), Here N is the number of items in the list.
Auxiliary Space: O(1), As constant extra space is used.

Method : Using for loop

STEP BY STEP APPROACH :

  1. Initialize a list of numbers called list1 with integer values.
  2. Initialize two variables pos_count and neg_count to 0.
  3. Convert all the numbers in list1 to strings using the map() function and store them in a new list called list2.
  4. Iterate through each number num in list2 using a for loop.
  5. For each number num, check the first character of the string representation of the number using num[0]:
    a. If the first character is “-“, increment the neg_count variable as the number is negative.
    b. Else, check if the number is not equal to “0” using num!=”0″:
    i. If the number is positive, increment the pos_count variable.
  6. Print the final values of pos_count and neg_count using the print() function.

Python3




# Python program to count positive and negative numbers in a List
 
# list of numbers
list1 = [10, -21, 4, -45, 66, -93, 1,0]
 
pos_count, neg_count = 0, 0
list2=list(map(str,list1))
 
# iterating each number in list
for num in list2:
    # checking condition
    if num[0]=="-":
        neg_count += 1
    elif(num!="0"):
        if(not num[0]=="-"):
            pos_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

Time Complexity: O(N)
Auxiliary Space: O(N)

Method: Using the collections module and the Counter() function:

  • Importing the Counter class from the collections module.
  • Using a list comprehension to get a list of boolean values to check whether each number is non-negative or not, based on whether it is greater than or equal to zero.
  • Passing the list of booleans to the Counter to get a dictionary mapping for counts.
  • Storing the count of positive and negative no.
  • Printing the output.

Python3




# Python program to count positive and negative numbers in a List
 
from collections import Counter
 
# list of numbers
list1 = [10, -21, 4, -45, 66, -93, 1,0]
 
# use the Counter module to count the number of positive and negative numbers in the list
counts = Counter(num > 0 for num in list1)
 
# get the count of positive and negative numbers excluding 0 from positive as well as negative
pos_count = counts[True]
neg_count = counts[False] - (0 in 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

Time Complexity: O(N) as we are traversing the whole list which takes O(N) time complexity.
Auxiliary Space: O(1) as no extra memory is used.



Similar Reads

Python Program to find Sum of Negative, Positive Even and Positive Odd numbers in a List
Given a list. The task is to find the sum of Negative, Positive Even, and Positive Odd numbers present in the List. Examples: Input: -7 5 60 -34 1 Output: Sum of negative numbers is -41 Sum of even positive numbers is 60 Sum of odd positive numbers is 6 Input: 1 -1 50 -2 0 -3 Output: Sum of negative numbers is -6 Sum of even positive numbers is 50
7 min read
C program to count Positive and Negative numbers in an Array
Given an array arr of integers of size N, the task is to find the count of positive numbers and negative numbers in the array Examples: Input: arr[] = {2, -1, 5, 6, 0, -3} Output: Positive elements = 3 Negative elements = 2 There are 3 positive, 2 negative, and 1 zero. Input: arr[] = {4, 0, -2, -9, -7, 1} Output: Positive elements = 2 Negative elem
2 min read
Only integer with positive value in positive negative value in array
Given an array of N integers. In the given, for each positive element 'x' there exist a negative value '-x', except one integer whose negative value is not present. That integer may occur multiple number of time. The task is find that integer. Examples: Input : arr[] = { 1, 8, -6, -1, 6, 8, 8 } Output : 8 All positive elements have an equal negativ
8 min read
Python Program to Rearrange positive and negative numbers in O(n) time and O(1) extra space
An array contains both positive and negative numbers in random order. Rearrange the array elements so that positive and negative numbers are placed alternatively. Number of positive and negative numbers need not be equal. If there are more positive numbers they appear at the end of the array. If there are more negative numbers, they too appear in t
3 min read
Find ratio of zeroes, positive numbers and negative numbers in the Array
Given an array a of integers of size N integers, the task is to find the ratio of positive numbers, negative numbers and zeros in the array up to four decimal places. Examples: Input: a[] = {2, -1, 5, 6, 0, -3} Output: 0.5000 0.3333 0.1667 There are 3 positive, 2 negative, and 1 zero. Their ratio would be positive: 3/6 = 0.5000, negative: 2/6 = 0.3
7 min read
Rearrange Array in negative numbers, zero and then positive numbers order
Given an arr[ ] of size N, containing positive, negative integers and one zero. The task is to rearrange the array in such a way that all negative numbers are on the left of 0 and all positive numbers are on the right. Note: It is not mandatory to maintain the order of the numbers. If there are multiple possible answers, print any of them. Examples
6 min read
Java Program for Rearrange positive and negative numbers in O(n) time and O(1) extra space
An array contains both positive and negative numbers in random order. Rearrange the array elements so that positive and negative numbers are placed alternatively. Number of positive and negative numbers need not be equal. If there are more positive numbers they appear at the end of the array. If there are more negative numbers, they too appear in t
3 min read
Javascript Program to Rearrange positive and negative numbers in O(n) time and O(1) extra space
An array contains both positive and negative numbers in random order. Rearrange the array elements so that positive and negative numbers are placed alternatively. Number of positive and negative numbers need not be equal. If there are more positive numbers they appear at the end of the array. If there are more negative numbers, they too appear in t
3 min read
C++ Program to Rearrange positive and negative numbers in O(n) time and O(1) extra space
An array contains both positive and negative numbers in random order. Rearrange the array elements so that positive and negative numbers are placed alternatively. Number of positive and negative numbers need not be equal. If there are more positive numbers they appear at the end of the array. If there are more negative numbers, they too appear in t
4 min read
C Program to Rearrange positive and negative numbers in O(n) time and O(1) extra space
An array contains both positive and negative numbers in random order. Rearrange the array elements so that positive and negative numbers are placed alternatively. Number of positive and negative numbers need not be equal. If there are more positive numbers they appear at the end of the array. If there are more negative numbers, they too appear in t
3 min read