Python program to print negative numbers in a list
Last Updated :
02 May, 2023
Given a list of numbers, write a Python program to print all negative numbers in the given list.
Example:
Input: list1 = [12, -7, 5, 64, -14]
Output: -7, -14
Input: list2 = [12, 14, -95, 3]
Output: -95
Example #1: Print all negative numbers from the given list using for loop Iterate each element in the list using for loop and check if the number is less than 0. If the condition satisfies, then only print the number.
Python3
list1 = [ 11 , - 21 , 0 , 45 , 66 , - 93 ]
for num in list1:
if num < 0 :
print (num, end = " " )
|
Output:
-21 -93
Time complexity: O(n)
Auxiliary space: O(n), where n is total number of values in list1.
Example #2: Using while loop
Python3
list1 = [ - 10 , 21 , - 4 , - 45 , - 66 , 93 ]
num = 0
while (num < len (list1)):
if list1[num] < 0 :
print (list1[num], end = " " )
num + = 1
|
Time complexity: O(n), where n is the length of the input list.
Auxiliary space: O(1).
Example #3: Using list comprehension
Python3
list1 = [ - 10 , - 21 , - 4 , 45 , - 66 , 93 ]
neg_nos = [num for num in list1 if num < 0 ]
print ( "Negative numbers in the list: " , * neg_nos)
|
Output
Negative numbers in the list: -10 -21 -4 -66
Time complexity: O(n), where n is the number of elements in the input list.
Auxiliary space: O(n), as it creates a new list to store the negative numbers in the input list.
Example #4: Using lambda expressions
Python3
list1 = [ - 10 , 21 , 4 , - 45 , - 66 , 93 , - 11 ]
neg_nos = list ( filter ( lambda x: (x < 0 ), list1))
print ( "Negative numbers in the list: " , * neg_nos)
|
Output
Negative numbers in the list: -10 -45 -66 -11
Time complexity: O(n), where n is the number of elements in the list.
Auxiliary space: O(m), where m is the number of negative numbers in the list.
Method: Using enumerate function
Python3
l = [ 12 , - 7 , 5 , 64 , - 14 ]
print ([a for j,a in enumerate (l) if a< 0 ])
|
Method: Using startswith() method
Python3
list1 = [ 11 , - 21 , 0 , 45 , 66 , - 93 ]
res = []
list2 = list ( map ( str , list1))
for i in range ( 0 , len (list2)):
if (list2[i].startswith( "-" )):
res.append( str (list1[i]))
res = " " .join(res)
print (res)
|
The time complexity of the given program is O(n), where n is the length of the input list.
The auxiliary space complexity of the program is also O(n), where n is the length of the input list.
Method: Using numpy.array
Python3
import numpy as np
list1 = [ - 10 , 21 , 4 , - 45 , - 66 , 93 , - 11 ]
temp = np.array(list1)
neg_nos = temp[temp < = 0 ]
print ( "Negative numbers in the list: " , * neg_nos)
|
Output:
Negative numbers in the list: -10 -45 -66 -11
Time Complexity: O(n)
Auxiliary Space: O(n)
Method : Using Recursion
Python3
def PrintNegative(itr,list1):
if itr = = len (list1):
return
if list1[itr] < 0 :
print ( list1[itr],end = " " )
PrintNegative(itr + 1 ,list1)
list1 = [ - 1 , 8 , 9 , - 5 , 7 ]
PrintNegative( 0 ,list1)
|
Method: Using numpy.array() and numpy.where()
note: install numpy module using command “pip install numpy”
Algorithm:
Convert the input list to a numpy array using numpy.array() method.
Use numpy.where() method to find all the negative numbers in the array.
Print the negative numbers.
Python3
import numpy as np
list1 = [ 12 , - 7 , 5 , 64 , - 14 ]
arr1 = np.array(list1)
neg_nums = arr1[np.where(arr1 < 0 )]
print ( "Negative numbers in the list: " , * neg_nums)
|
Output:
Negative numbers in the list: -7 -14
Time complexity: O(n), where n is the length of the input list.
Auxiliary space: O(n), as we are creating a numpy array to store the input list and another numpy array to store the negative numbers.
Method : Using functools.reduce
Algorithm:
- Initialize the test list.
- Use reduce function with the lambda function which adds a negative element to the list.
- Print list.
Python3
from functools import reduce
list1 = [ - 10 , - 21 , - 4 , 45 , - 66 , 93 ]
neg_nos = reduce ( lambda a, b : a + [ b ] if b < 0 else a ,list1, [])
print ( "Negative numbers in the list: " , * neg_nos)
|
Output
Negative numbers in the list: -10 -21 -4 -66
Time complexity: O(N), where N is the length of the test list.
Auxiliary space: O(M), where M is length of the negative number list.
Please Login to comment...