Dark Mode On/Off
PUBLISHED ON: JULY 1, 2021

# Python program to print negative numbers in a list

In this tutorial, we will write a Python program to find and print all the negative numbers in a list. The List is an ordered set of values enclosed in square brackets [ ]. List stores some values called elements in it, which can be accessed by their particular index.

Any number which is below zero is known as a negative number. Negative numbers can be written with a '-' sign in front of them. We will be following various approaches to find and print all the negative numbers in a list.

Input: [2, 6, -10, -3, 1, -9]

Output: [-10, -3, -9]

Input: [-2, -4, 5, -3, 0, 6, -10]

Output: [-2, -4, -3, -10]

## Approach to print negative numbers in a list

To execute this task we can follow various approaches, which will be discussed in detail below

1. traversing the list and checking each element if it is negative
2. using list comprehension
3. using lambda functions

## Approach 1: Traversing and checking

In this approach, we will use for loop to traverse the list, and with the help of if condition, we will check if the number in each iteration is less than 0. The numbers which will satisfy the given condition will be printed.

### Algorithm

Follow the algorithm to understand the approach better

Step 1- Take input of list elements from the user

Step 2- Add the elements to the list

Step 3- To print the negative elements, run a loop for each element

Step 4- Check if the element is less than 0

Step 5- Numbers that satisfy the condition will be printed

### Python Program

Look at the program to understand the implementation of the above-mentioned approach. In this program, we have taken an input of elements of the list from the user and by using append(), added the elements to the list. append() is a built-in function in the Python library.

``````# print negative Numbers in a List

# input of list
li=[]
n=int(input("Enter size of list "))
for i in range(0,n):
e=int(input("Enter element of list "))
li.append(e)

print("negative numbers in",li,"are: ")

#traversing
for i in li:

# checking condition
if i<0:
print(i, end = " ")``````

Enter size of list 6
Enter element of list 0
Enter element of list -3
Enter element of list 4
Enter element of list -6
Enter element of list -7
Enter element of list -2
negative numbers in [0, -3, 4, -6, -7, -2] are:
-3 -6 -7 -2

## Approach 2: using list comprehension

In this approach, we will use the list comprehension method. List comprehension is a shorter syntax for creating a new list based on the values of an existing list. We will add the negative numbers from the list into the new list with the help of list comprehension.

### Algorithm

Follow the algorithm to understand the approach better

Step 1- Take input of list elements from the user

Step 2- Add the elements to the list

Step 3- To print the negative numbers, use list comprehension

Step 4- Check if the element is lesser than 0

Step 5- If the number satisfies the condition store it in a new list

Step 6- Print the new list

### Python Program 2

In this program, we have taken an input of elements of the list from the user and using append() which is a built-in function in the Python library, added the elements to the list. To print the numbers with space we have used end=" ".

``````# print Negative Numbers in a List

# input of list
li=[]
n=int(input("Enter size of list "))
for i in range(0,n):
e=int(input("Enter element of list "))
li.append(e)

print("Negative numbers in",li,"are: ")

# using list comprehension
negative_num = [num for num in li if num < 0]

print(negative_num)``````

Enter size of list 6
Enter element of list 3
Enter element of list 9
Enter element of list -3
Enter element of list 0
Enter element of list -2
Enter element of list -5
Negative numbers in [3, 9, -3, 0, -2, -5] are:
[-3, -2, -5]

## Approach 3: using lambda function

In this approach, we will be using lambda function to display positive numbers in a list. A lambda function is a small anonymous function. It can take any number of arguments, but can only have one expression.

### Algorithm

Follow the algorithm to understand the approach better

Step 1- Take input of list elements from the user

Step 2- Add the elements to the list

Step 3- To print the negative numbers, use the lambda function

Step 4- Give the condition as- number in the list is less than 0

Step 5- Numbers that satisfy the condition will be stored in a new list

Step 6- Print the new list

### Python Program 3

We have defined a lambda function that will store all the numbers in the given list which are less than 0 to the new list.

``````# print Negative Numbers in a List

# input of list
li=[]
n=int(input("Enter size of list "))
for i in range(0,n):
e=int(input("Enter element of list "))
li.append(e)

print("Negative numbers in",li,"are: ")

# using lambda function
negative_num = list(filter(lambda x: (x<0),li))
print(negative_num)``````

Enter size of list 6
Enter element of list 0
Enter element of list -3
Enter element of list -4
Enter element of list 2
Enter element of list 7
Enter element of list -1
Negative numbers in [0, -3, -4, 2, 7, -1] are:
[-3, -4, -1]

## Conclusion

In this tutorial, we have learned three approaches for finding and displaying all the negative numbers in a list. We have discussed the use of loops, conditional statements, list comprehension, and lambda functions in this tutorial.

Want to learn coding?
Try our new interactive courses.
Over 20,000+ students enrolled.