Removing Odd Elements From List in Python


In Python, lists are a versatile data structure that allows you to store and manipulate collections of items. There may be use cases where you need to remove specific elements from a list based on certain criteria. In this tutorial, we will learn how to remove odd elements from a list in Python.

Using a For Loop and remove() method

The approach is to use a for loop to iterate through each element of the list and check if the element is odd or even and then use the remove() method to remove it from the list.

Syntax

remove()

remove(item)

Parameter

  • Item:Item to be removed from the list.

Explanation

  • Create a list with a combination of odd and even numbers.

  • Iterate through the entire list using for loop.

  • For each element, check if it is an odd number or an even number.

  • If the number is odd then remove it from the list using remove() method

Example

arr = [1, 2, 3, 4, 5, 6]
print("Original List : ",arr)
for i in arr:
    # checking if the number is odd number
    if(i%2==1):
        # removing from the list.
        arr.remove(i)
print("List after odd elements removed : ",arr)

Output

Original List :  [1, 2, 3, 4, 5, 6]
List after odd elements removed :  [2 4 6]

Using List Comprehension

List comprehension provides a way to iterate through the list of elements and perform checks to validate if the element is odd or even.

Syntax

[expression for element in iterable]
  • Iterable :It can be a list, set, tuple, or any Python iterable.

  • Element: item present in the iterable.

  • Expression: Operation that we want to perform on the element

Explanation

  • Create a list of elements.

  • Use the list comprehensions to iterate over the list and add it to the list only if it is not an odd number.

Example

arr = [1, 2, 3, 4, 5, 6]

print("Original List : ",arr)

arr = [i for i in arr if i%2==0]

print("List after odd elements removed : ",arr)

Output

Original List :  [1, 2, 3, 4, 5, 6]
List after odd elements removed :  [2 4 6]

Using the filter() Function

The filter() function in Python is a built-in function that creates a new iterable object by filtering elements from an iterable based on a condition. It takes two arguments: a function that defines the condition and an iterable with the elements to be filtered.

Syntax

filter(function, iterable)
  • Iterable: The sequence of items to which the specified function will be applied.

  • Function: The function we want to apply to the items in the iterator.

Explanation

  • Create a list of elements.

  • We will use a lambda function to check if the element is odd or even.

  • Pass the above mentioned lamba function and the list to the filter method.

  • The filter method will return a iterable, convert it to a list using list() method..

Example

arr = [1, 2, 3, 4, 5, 6]

print("Original List : ",arr)

arr = list(filter(lambda i: i % 2 == 0, arr))

print("List after odd elements removed : ",arr)

Output

Original List :  [1, 2, 3, 4, 5, 6]
List after odd elements removed :  [2 4 6]

Using the numpy

The numpy arrays has advanced filtering capabilities. Numpy arrays can be applied with boolean mask and elements having True will be retained, others will be removed. In this case to get the boolean mask array, just divide the array by 2, it will return a boolean array indicating which elements are even and which are odd.

Syntax

The np.array() function in NumPy is used to create a new NumPy array from an iterable object.

numpy.array(iterable)
  • terable: list or a tuple to be coveted to numpy array.

Explanation

  • Create a list of elements and convert it to numpy array using numpy.array().

  • Create a boolean mask array by dividing the numpy array by 2

  • Now pass the created boolean array to the the numpy array as an index, this will only return the even numbers.

Example

import numpy as np
arr = [1, 2, 3, 4, 5, 6]

print("Original List : ",arr)

arr = np.array(arr)
arr = arr[arr % 2 == 0]

print("List after odd elements removed : ",arr)

Output

Original List :  [1, 2, 3, 4, 5, 6]
List after odd elements removed :  [2 4 6]

Conclusion

In this tutorial, we have explored multiple approaches to remove the odd elements from a list, including list comprehension, loops, the filter function and advanced filtering capabilities of the NumPy library.

Updated on: 09-Aug-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements