In this article, we will learn about the solution and approach to solve the given problem statement.
Problem statement
Given a list iterable as input, we need to display odd numbers in the given iterable.
Here we will be discussing three different approaches to solve this problem.
Approach 1 − Using enhanced for loop
Example
list1 = [11,23,45,23,64,22,11,24] # iteration for num in list1: # check if num % 2 != 0: print(num, end = " ")
Output
11, 23, 45, 23, 11
Approach 2 − Using lambda & filter functions
Example
list1 = [11,23,45,23,64,22,11,24] # lambda exp. odd_no = list(filter(lambda x: (x % 2 != 0), list1)) print("Odd numbers in the list: ", odd_no)
Output
Odd numbers in the list: [11, 23, 45, 23, 11]
Approach 3 − Using the list comprehension
Example
list1 = [11,23,45,23,64,22,11,24] #list comprehension odd_nos = [num for num in list1 if num % 2 != 0] print("Odd numbers : ", odd_nos)
Output
Odd numbers in the list: [11, 23, 45, 23, 11]
Conclusion
In this article, we learned about the approach to find all odd numbers in the list given as input.