Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python program to print all negative numbers in a range
Sometimes you need to extract only the negative numbers from a range. Python provides several methods to filter negative numbers: using loops, list comprehension, and built-in functions like filter().
Method 1: Using a Loop to Separate Negative Numbers
Create a separate list for negative numbers and append them using a loop ?
low_num = -10
high_num = 15
main_numbers = list(range(low_num, high_num + 1))
negative_numbers = []
print("Original range:", main_numbers)
# Separate negative numbers into a new list
for num in main_numbers:
if num < 0:
negative_numbers.append(num)
print("Negative numbers:", negative_numbers)
Original range: [-10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15] Negative numbers: [-10, -9, -8, -7, -6, -5, -4, -3, -2, -1]
Method 2: Using List Comprehension
Filter negative numbers directly using list comprehension for more concise code ?
low_num = -8
high_num = 12
main_numbers = list(range(low_num, high_num + 1))
print("Original range:", main_numbers)
# Filter negative numbers using list comprehension
negative_numbers = [num for num in main_numbers if num < 0]
print("Negative numbers:", negative_numbers)
Original range: [-8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] Negative numbers: [-8, -7, -6, -5, -4, -3, -2, -1]
Method 3: Using filter() Function
Use the filter() function with a lambda expression for functional programming approach ?
low_num = -6
high_num = 10
main_numbers = list(range(low_num, high_num + 1))
print("Original range:", main_numbers)
# Filter negative numbers using filter() and lambda
negative_numbers = list(filter(lambda x: x < 0, main_numbers))
print("Negative numbers:", negative_numbers)
Original range: [-6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] Negative numbers: [-6, -5, -4, -3, -2, -1]
Method 4: Direct Range Filtering
Create negative numbers directly by limiting the range to negative values only ?
low_num = -12
high_num = 8
print("Full range:", list(range(low_num, high_num + 1)))
# Get only negative numbers by limiting range to 0
negative_numbers = list(range(low_num, 0))
print("Negative numbers:", negative_numbers)
Full range: [-12, -11, -10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8] Negative numbers: [-12, -11, -10, -9, -8, -7, -6, -5, -4, -3, -2, -1]
Comparison
| Method | Code Length | Performance | Best For |
|---|---|---|---|
| Loop | Longer | Good | Complex filtering logic |
| List Comprehension | Concise | Fast | Simple conditions |
| filter() | Medium | Good | Functional programming |
| Direct Range | Shortest | Fastest | Known boundaries |
Conclusion
Use list comprehension for readable and efficient negative number filtering. For known ranges, direct range limiting is the most efficient approach. Choose the method that best fits your specific use case and coding style.
