

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 Questions & Answers
- 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
- Lambda expression in Python to rearrange positive and negative numbers
- Java Program to convert positive int to negative and negative to positive
- Python program to print negative numbers in a list
- Reversing negative and positive numbers in JavaScript
- Python program to Count Even and Odd numbers in a List
- Sum a negative number (negative and positive digits) - JavaScript
- Implement Bubble sort with negative and positive numbers – JavaScript?
- Split an array of numbers and push positive numbers to JavaScript array and negative numbers to another?
- Rearrange positive and negative numbers using inbuilt sort function in C++
- Rearrange positive and negative numbers with constant extra space in C++
- Python Program to Check if a Number is Positive, Negative or 0
- Python - Sum negative and positive values using GroupBy in Pandas
Advertisements