Python program to print odd numbers in a List
Last Updated :
29 Apr, 2023
Given a list of numbers, write a Python program to print all odd numbers in the given list.
Example:
Input: list1 = [2, 7, 5, 64, 14]
Output: [7, 5]
Input: list2 = [12, 14, 95, 3, 73]
Output: [95, 3, 73]
Using for loop : Iterate each element in the list using for loop and check if num % 2 != 0. If the condition satisfies, then only print the number.
Python3
list1 = [ 10 , 21 , 4 , 45 , 66 , 93 ]
for num in list1:
if num % 2 ! = 0 :
print (num, end = " " )
|
Time Complexity: O(N)
Auxiliary Space: O(1), As constant extra space is used.
Using while loop :
Python3
list1 = [ 10 , 21 , 4 , 45 , 66 , 93 ]
i = 0
while (i < len (list1)):
if list1[i] % 2 ! = 0 :
print (list1[i], end = " " )
i + = 1
|
Time Complexity: O(N)
Auxiliary Space: O(1), As constant extra space is used.
Using list comprehension :
Python3
list1 = [ 10 , 21 , 4 , 45 , 66 , 93 ]
only_odd = [num for num in list1 if num % 2 = = 1 ]
print (only_odd)
|
Time Complexity: O(N)
Auxiliary Space: O(1), As constant extra space is used.
Using lambda expressions :
Python3
list1 = [ 10 , 21 , 4 , 45 , 66 , 93 , 11 ]
odd_nos = list ( filter ( lambda x: (x % 2 ! = 0 ), list1))
print ( "Odd numbers in the list:" , odd_nos)
|
Output
Odd numbers in the list: [21, 45, 93, 11]
Time Complexity: O(N)
Auxiliary Space: O(1), As constant extra space is used.
Method: Using pass
Python3
lst = [ 10 , 21 , 4 , 45 , 66 , 93 , 11 ]
for i in lst:
if i % 2 = = 0 :
pass
else :
print (i, end = " " )
|
Time Complexity: O(N)
Auxiliary Space: O(1), As constant extra space is used.
Method: Using recursion
Python3
def oddnumbers( list , n = 0 ):
if n = = len ( list ):
exit()
if list [n] % 2 ! = 0 :
print ( list [n], end = " " )
oddnumbers( list , n + 1 )
list1 = [ 10 , 21 , 4 , 45 , 66 , 93 , 11 ]
print ( "odd numbers in the list:" , end = " " )
oddnumbers(list1)
|
Output
odd numbers in the list: 21 45 93 11
Time Complexity: O(N)
Auxiliary Space: O(1), As the function is tail recursive no extra stack space is used.
Method: Using enumerate function
Python3
list1 = [ 2 , 7 , 5 , 64 , 14 ]
for a,i in enumerate (list1):
if i % 2 ! = 0 :
print (i,end = " " )
|
Time Complexity: O(N)
Auxiliary Space: O(1), As constant extra space is used.
Method: Using numpy.array function
Python3
import numpy as np
list1 = np.array([ 10 , 21 , 4 , 45 , 66 , 93 ])
only_odd = list1[list1 % 2 = = 1 ]
print (only_odd)
|
Output:
[21 45 93]
Time Complexity: O(N)
Auxiliary Space: O(1), As constant extra space is used.
Method: Using bitwise & operator
We can also find the number odd or not using & operator. We iterate the through the list using for loop. If num & 1
==1.If the condition satisfies print element.
Python3
list1 = [ 9 , 5 , 4 , 7 , 2 ]
for ele in list1:
if ele & 1 :
print (ele,end = " " )
|
Time Complexity: O(N)
Auxiliary Space: O(1), As constant extra space is used.
Method: Using bitwise | operator
We can also find the number odd or not using | operator. We iterate the through the list using for loop. If num | 1
==num. If the condition satisfies print element.
Python3
list1 = [ 9 , 5 , 4 , 7 , 2 ]
for ele in list1:
if ele | 1 = = ele:
print (ele,end = " " )
|
Time Complexity: O(N)
Auxiliary Space: O(1), As constant extra space is used.
Using filter function :
Python3
def is_odd(number):
return number % 2 = = 1
def print_odd_numbers(numbers):
odd_numbers = list ( filter (is_odd, numbers))
return odd_numbers
numbers = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ]
print (print_odd_numbers(numbers))
|
Output:
[1, 3, 5, 7, 9]
Time Complexity: O(N)
Auxiliary Space: O(1), As constant extra space is used.
Using numpy.where()
note: install numpy module using command “pip install numpy”
The following program prints all odd numbers in a given list using the numpy.where() function.
Algorithm:
Create a list of numbers.
Convert the list to a numpy array using the numpy.array() function.
Find the indices of elements in the array that are odd using the numpy.where() function.
Extract the elements of the array at the odd indices.
Print the extracted elements.
Python3
import numpy as np
list1 = [ 10 , 21 , 4 , 45 , 66 , 93 ]
arr = np.array(list1)
idx = np.where(arr % 2 ! = 0 )
only_odd = arr[idx]
print (only_odd)
|
Output:
[21 45 93]
Time Complexity: O(N), where N is the number of elements in the list.
Auxiliary Space: O(N), as the entire list is converted to a numpy array. However, the space used by idx and only_odd is proportional to the number of odd elements in the list, which is typically much smaller than N.
Method: Using functools.reduce method
Algorithm:
- Initialize an array.
- reduce method takes a function, original list and initial value.
- The function on reduce method checks the number is odd for each element of the original list.
- If the element is odd then add it to an initial value which is an empty array.
Python
from functools import reduce
list1 = [ 10 , 21 , 4 , 45 , 66 , 93 ]
odd_list = reduce ( lambda a, b : a + [b] if b % 2 else a, list1, [])
print (odd_list)
|
Time Complexity: O(n), where n is the number of elements in the list.
Auxiliary Space: O(k), Where k is number of odd element in list. Because a list containing an odd elements is used.
Please Login to comment...