 Open in App
Not now

# Python program to count positive and negative numbers in a list

• Last Updated : 14 Dec, 2022

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.

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

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

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

My Personal Notes arrow_drop_up