Python program to print odd numbers in a list

In this article, we will learn different approaches to find and print odd numbers from a list. An odd number is any integer that is not divisible by 2, meaning when divided by 2, it leaves a remainder of 1.

Problem Statement

Given a list of integers as input, we need to display all odd numbers present in the list. We will explore three different approaches to solve this problem efficiently.

Using For Loop

The most straightforward approach is to iterate through each number and check if it's odd using the modulo operator ?

numbers = [11, 23, 45, 23, 64, 22, 11, 24]

# Iterate through the list
for num in numbers:
    # Check if number is odd
    if num % 2 != 0:
        print(num, end=" ")
11 23 45 23 11 

Using Lambda and Filter Functions

Python's filter() function combined with a lambda expression provides a functional programming approach to filter odd numbers ?

numbers = [11, 23, 45, 23, 64, 22, 11, 24]

# Using lambda and filter
odd_numbers = list(filter(lambda x: (x % 2 != 0), numbers))
print("Odd numbers in the list:", odd_numbers)
Odd numbers in the list: [11, 23, 45, 23, 11]

Using List Comprehension

List comprehension offers a concise and Pythonic way to filter odd numbers in a single line ?

numbers = [11, 23, 45, 23, 64, 22, 11, 24]

# List comprehension
odd_numbers = [num for num in numbers if num % 2 != 0]
print("Odd numbers:", odd_numbers)
Odd numbers: [11, 23, 45, 23, 11]

Comparison

Method Readability Performance Best For
For Loop High Good Beginners, custom logic
Filter + Lambda Medium Good Functional programming
List Comprehension High Best Pythonic, concise code

Conclusion

List comprehension is generally preferred for filtering odd numbers due to its readability and performance. Use for loops when you need additional processing logic, and filter() for functional programming approaches.

Updated on: 2026-03-25T06:37:52+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements