- 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
Python program to count positive and negative numbers in a list
In this article, we will learn about the solution and approach to solve the given problem statement.
Problem statement
Given a list iterable we need to count all the positive and negative numbers available in the iterable.
Her we will be discussing two approaches −
- Brute-force approach
- Using lambda inline function
Approach 1 − Brute-force method
Example
list1 = [1,-9,15,-16,13] pos_count, neg_count = 0, 0 for num in list1: if num >= 0: pos_count += 1 else: neg_count += 1 print("Positive numbers : ", pos_count) print("Negative numbers : ", neg_count)
Output
Positive numbers : 3 Negative numbers : 2
Approach 2 − Using lambda & filter functions
Example
list1 = [1,-9,15,-16,13] neg_count = len(list(filter(lambda x: (x < 0), list1))) pos_count = len(list(filter(lambda x: (x >= 0), list1))) print("Positive numbers : ", pos_count) print("Negative numbers : ", neg_count)
Output
Positive numbers : 3 Negative numbers : 2
Conclusion
In this article, we learned about the approach to count positive & negative numbers in a list.
- Related Articles
- Count positive and negative numbers in a list in Python program
- 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
- Distinguish positive and negative numbers.
- Java Program to convert positive int to negative and negative to positive
- Reversing negative and positive numbers in JavaScript
- Explain the difference between positive and negative numbers.
- Python Program to Check if a Number is Positive, Negative or 0
- 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?
- Write a program in Python to shift a dataframe index by two periods in positive and negative direction

Advertisements