A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/python-program-to-print-negative-numbers-in-a-list/ below:

Python program to print negative numbers in a list

Python program to print negative numbers in a list

Last Updated : 11 Jul, 2025

In Python, it’s often necessary to find and print negative numbers from a list. In this article we will explore various approches to print negative numbers in a list. The most basic method for printing negative numbers is to use a for loop to iterate through the list and check each element.

Python
a = [5, -3, 7, -1, 2, -9, 4]

# Loop through the list and print negative numbers
for num in a:
    if num < 0:
        print(num)

Let's explore other method to print negative numbers in a list:

Using List Comprehension

List comprehension is a more efficient way to extract negative numbers from a list. It allows us to write the same logic in a more compact form.

Python
a = [5, -3, 7, -1, 2, -9, 4]

# Using list comprehension to filter negative numbers
n = [num for num in a if num < 0]

print(n)
Using the Filter Function

filter() function is another efficient way to extract negative numbers. It allows us to filter elements from a list based on a condition.

Python
a = [5, -3, 7, -1, 2, -9, 4]

# Using filter function to find negative numbers
n = list(filter(lambda x: x < 0, a))
print(n)

Explanation:

Using the map() Function

map() function applies a function to all items in the list. We can use map() to filter negative numbers, though it requires an extra step of converting the result back into a list and checking for negative numbers.

Python
a = [5, -3, 7, -1, 2, -9, 4]

# Using map to filter negative numbers
n = list(map(lambda x: x if x < 0 else None, a))

# Removing None values and printing negative numbers
n = [num for num in n if num is not None]
print(n)

Python Program to Print Negative Numbers in a Range


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4