In this article, we will learn to write a python program to find and print all the negative numbers present in the python list.
We know that the list is an ordered set of values enclosed in square brackets [ ]. We can access the elements in the list by their particular index. A number that is less than zero is known as a negative number. Negative numbers are written with the hyphen(-) sign in front of them.
Input: list1 = [1,-5,7,-3,-6,4]
output: -5,-3,-6
Input: list2 = [-15,20,22,-33,4]
output:-15,-33
We will use the following approaches to solve this problem.
lambda expression
Let's understand the topic with the help of some simple examples.
Here, we will use for loop to traverse each element of the list and fetch the negative elements. An algorithm is given below.
Step 1: Start
Step 2: Create a list of numbers
Step 3: Start a for loop
Step 4: Add an if condition to check the negative element
Step 5: If the condition true, print that number
Step 6: No output
Step 7: End
Look at the program below to understand the implementation of the above-mentioned algorithm.
# Python program to print negative numbers in a list
# list of numbers
list1 = [1, -5, 7, -3, -6, 4]
# iterating each number in the list
for num in list1:
# checking the condition
if num < 0:
print(num)
Output:
-5
-3
-6
Here, we used another approach to solve the problem, we have taken an input of elements of the list from the user and then by using python built-in function append(), we have added all the elements to the list. And then to print negative numbers, we have done traversing and checking with the help of for loop and if statement.
# Python program to print negative numbers in a list
# Empty list for storing input elements
list1 = []
# Size of the list
n = int(input("Enter size of list: "))
# Create a for loop
for i in range(0, n):
# Take elements of list from user
ele_input = int(input(f"Enter the element {i+1}: "))
# Append all the element in the list1
list1.append(ele_input)
print("Negative numbers in", list1, "are: ")
# iterating each number in list
for i in list1:
# checking condition
if i < 0:
print(i, end=" ")
Output:
Enter size of list: 5
Enter the element 1: 23
Enter the element 2: -34
Enter the element 3: -53
Enter the element 4: 32
Enter the element 5: -54
Negative numbers in [23, -34, -53, 32, -54] are:
-34 -53 -54
In this approach, we are going to use the list comprehension method to print negative numbers in a list. List comprehension is an easy and elegant way to define and create a list in python. An algorithm is given below.
Step 1: Start
Step 2: Create a list of numbers by user input
Step 3: Apply list comprehension to find the negative numbers
Step 4: Provide if condition in the list comprehension to check whether the element is negative
Step 5: If the number satisfies the condition, store it in a new list
Step 6: Print the new list
Step 7: End
Here, we have created a list of numbers and then by using the above-mentioned algorithm, we printed all the negative numbers present in that list.
# Python program to print negative numbers in a list
# list of numbers
list1 = [1, -5, 7, -3, -6, 4]
# using list comprehension
negative_num = [num for num in list1 if num < 0]
print("Negative numbers in the list: ", *negative_num)
Output:
Negative numbers in the list: -5 -3 -6
Here, we will use the lambda function to find negative elements of a list. In python, the lambda function is an anonymous function that means the function without a name. An algorithm is given below.
Step 1: Start
Step 1: Create a list of numbers
Step 2: Create a lambda expression to print negative elements
Step 3: Provide condition the lambda expression to check whether the list contains any negative number
Step 4: If the number satisfies the condition, store it in a new list
Step 5: Print the new list
Step 6: End
Here, we have created a list of numbers and then by using the above-mentioned algorithm, we printed all the negative numbers present in that list. See the Python code below.
# Python program to print negative numbers in a list
# list of numbers
list1 = [1, -5, 7, -3, -6, 4]
# print negative numbers using lambda expression.
negative_num = list(filter(lambda x: (x < 0), list1))
print("Negative numbers in the list: ", *negative_num)
Output:
Negative numbers in the list: -5 -3 -6