Open In App

Python program to print odd numbers in a List

Last Updated : 29 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
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. 



Similar Reads

Count of numbers of length N having prime numbers at odd indices and odd numbers at even indices
Given a number N, the task is to calculate the count of numbers of length N having prime numbers at odd indices and odd numbers at even indices. Example: Input : N = 1Output: 5Explanation : All valid numbers length 1 are 1, 3, 5, 7, 9, here we have only 1 odd index, therefore we have 5 valid numbers. Input: N = 2Output: 20 Explanation: There are 20
5 min read
Even numbers at even index and odd numbers at odd index
Given an array of size n containing equal number of odd and even numbers. The problem is to arrange the numbers in such a way that all the even numbers get the even index and odd numbers get the odd index. Required auxiliary space is O(1).Examples : Input : arr[] = {3, 6, 12, 1, 5, 8} Output : 6 3 12 1 8 5 Input : arr[] = {10, 9, 7, 18, 13, 19, 4,
11 min read
Make all the elements of array odd by incrementing odd-indexed elements of odd-length subarrays
Given an array arr[] of size N, the task is to make all the array elements odd by choosing an odd length subarray of arr[] and increment all odd positioned elements by 1 in this subarray. Print the count of such operations required. Examples: Input: arr[] = {2, 3, 4, 3, 5, 3, 2}Output: 2Explanation:In first operation, choose the subarray {2, 3, 4}
9 min read
Python program to print all odd numbers in a range
Given starting and endpoints, write a Python program to print all odd numbers in that given range. Example: Input: start = 4, end = 15 Output: 5, 7, 9, 11, 13, 15 Input: start = 3, end = 11 Output: 3, 5, 7, 9, 11 Example #1: Print all odd numbers from the given list using for loop Define the start and end limit of the range.Iterate from start till
7 min read
Print odd positioned nodes of odd levels in level order of the given binary tree
Given a binary tree, the task is to print the odd positioned nodes of odd levels in the level order traversal of the tree. The root is considered at level 0, and the leftmost node of any level is considered as a node at position 0.Example: Input: 1 / \ 2 3 / \ / \ 4 5 6 7 / \ 8 9 / \ 10 11 Output: 3 9 Input: 2 / \ 4 15 / / 45 17 Output: 15 Prerequi
8 min read
Python Program to Print Largest Even and Largest Odd Number in a List
Auxiliary Given a list. The task is to print the largest even and largest odd number in a list. Examples: Input: 1 3 5 8 6 10 Output: Largest even number is 10 Largest odd number is 5 Input: 123 234 236 694 809 Output: Largest odd number is 809 Largest even number is 694 The first approach uses two methods , one for computing largest even number an
7 min read
C++ program to print all Even and Odd numbers from 1 to N
Given a number N, the task is to print N even numbers and N odd numbers from 1. Examples: Input: N = 5 Output: Even: 2 4 6 8 10 Odd: 1 3 5 7 9 Input: N = 3 Output: Even: 2 4 6 Odd: 1 3 5 Approach: For Even numbers:Even numbers are numbers that are divisible by 2.To print even numbers from 1 to N, traverse each number from 1.Check if these numbers a
4 min read
Kth element in permutation of first N natural numbers having all even numbers placed before odd numbers in increasing order
Given two integers N and K, the task is to find the Kth element in the permutation of first N natural numbers arranged such that all the even numbers appear before the odd numbers in increasing order. Examples : Input: N = 10, K = 3 Output: 6Explanation:The required permutation is {2, 4, 6, 8, 10, 1, 3, 5, 7, 9}.The 3rd number in the permutation is
9 min read
Average of odd numbers till a given odd number
Given an odd number n, find the average of odd numbers from 1 to n.Examples: Input : n = 9 Output : 5 Explanation (1 + 3 + 5 + 7 + 9)/5 = 25/5 = 5 Input : n = 221 Output : 111 Method 1 We can calculate average by adding each odd numbers till n and then dividing sum by count.Below is the implementation of the approach. C/C++ Code // Program to find
8 min read
Count numbers from given range having odd digits at odd places and even digits at even places
Given two integers L and R, the task is to count numbers from the range [L, R] having odd digits at odd positions and even digits at even positions respectively. Examples: Input: L = 3, R = 25Output: 9Explanation: The numbers satisfying the conditions are 3, 5, 7, 9, 10, 12, 14, 16 and 18. Input: L = 128, R = 162Output: 7Explanation: The numbers sa
15+ min read