Open In App

Python program to print odd numbers in a List

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

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




# Python program to print odd Numbers in a List
 
# list of numbers
list1 = [10, 21, 4, 45, 66, 93]
 
# iterating each number in list
for num in list1:
 
    # checking condition
    if num % 2 != 0:
       print(num, end=" ")


Output

21 45 93 

Time Complexity: O(N)
Auxiliary Space: O(1), As constant extra space is used.

Using while loop : 

Python3




# Python program to print odd Numbers in a List
 
# list of numbers
list1 = [10, 21, 4, 45, 66, 93]
i = 0
 
# using while loop
while(i < len(list1)):
 
    # checking condition
    if list1[i] % 2 != 0:
        print(list1[i], end=" ")
 
    # increment i
    i += 1


Output

21 45 93 

Time Complexity: O(N)
Auxiliary Space: O(1), As constant extra space is used.

Using list comprehension

Python3




# Python program to print odd Numbers in a List
 
# list of numbers
list1 = [10, 21, 4, 45, 66, 93]
 
only_odd = [num for num in list1 if num % 2 == 1]
 
print(only_odd)


Output

[21, 45, 93]

Time Complexity: O(N)
Auxiliary Space: O(1), As constant extra space is used.

Using lambda expressions :

Python3




# Python program to print odd numbers in a List
 
# list of numbers
list1 = [10, 21, 4, 45, 66, 93, 11]
 
 
# we can also print odd no's using lambda exp.
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




# Python program to print odd numbers in a List
 
lst = [10, 21, 4, 45, 66, 93, 11]
for i in lst:
    if i % 2 == 0:
        pass
    else:
        print(i, end=" ")


Output

21 45 93 11 

Time Complexity: O(N)
Auxiliary Space: O(1), As constant extra space is used.

Method: Using recursion 

Python3




# Python program to print
# odd numbers in a list using recursion
 
 
def oddnumbers(list, n=0):
    # base case
    if n == len(list):
        exit()
    if list[n] % 2 != 0:
        print(list[n], end=" ")
    # calling function recursively
    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=" ")


Output

7 5 

Time Complexity: O(N)
Auxiliary Space: O(1), As constant extra space is used.

Method: Using numpy.array function

Python3




# Python program to print odd Numbers in a List
import numpy as np
# list of numbers
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




#List of numbers
list1 = [9,5,4,7,2]
 
for ele in list1:
  if ele & 1: #Checking the element odd or not
    print(ele,end=" ")


Output

9 5 7 

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




#List of numbers
list1 = [9,5,4,7,2]
 
for ele in list1:
  if ele | 1==ele: #Checking the element odd or not
    print(ele,end=" ")


Output

9 5 7 

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
 
# list of numbers
list1 = [10, 21, 4, 45, 66, 93]
 
# convert list to numpy array
arr = np.array(list1)
 
# find indices where elements are odd
idx = np.where(arr % 2 != 0)
 
# extract elements at odd indices
only_odd = arr[idx]
 
# print only odd elements
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




# Python program to print odd Numbers in a List
from functools import reduce
 
# list of numbers
list1 = [10, 21, 4, 45, 66, 93]
 
# Using reduce method in list
odd_list = reduce(lambda a, b : a + [b] if b%2 else a, list1, [])
 
print(odd_list)


Output

[21, 45, 93]

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. 



Last Updated : 29 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads