Skip to Content

Numpy – Get All Odd Elements in Array

The Numpy library in Python comes with a number of useful functions and methods to work with and manipulate the data in arrays. In this tutorial, we will look at how to get all the odd elements from a Numpy array with the help of some examples.

Steps to get all the odd values in a Numpy array

get all the odd values in a numpy array

You can use boolean indexing to filter the Numpy array such that the resulting array contains only the elements that specify a given condition. For example, values that are odd.

Step 1 – Create a Numpy array

First, we will create a Numpy array that we will be using throughout this tutorial.

import numpy as np

# create a numpy array
ar = np.array([1, 2, 3, 4, 5, 6, 7, 8])
# display the array
print(ar)

Output:

[1 2 3 4 5 6 7 8]

Here, we used the numpy.array() function to create a one-dimensional Numpy array containing some numbers.

Step 2 – Filter the array for odd values using a boolean expression

To get all the odd elements of a Numpy array, filter the array using boolean indexing.

Here, we will specify our boolean expression, ar % 2 != 0 and then use the boolean array resulting from this expression to filter our original array. The % (modulus) operator returns the remainder when the left value is divided by the right value. Since odd numbers are not divisible by 2, ar % 2 will not result in 0 if the number is odd.

Let’s get all the odd elements in the array created above

# odd values in the array ar
print(ar[ar % 2 != 0])

Output:

[1 3 5 7]

We get all the odd values in the array ar.

To understand what’s happening here, let’s look under the hood. Let’s see what we get from the expression ar % 2 != 0.

ar % 2 != 0

Output:

array([ True, False,  True, False,  True, False,  True, False])

We get a Numpy array with boolean values. The values in this array represent whether a value at a particular index satisfies the given condition or not (if the value is odd or not).

When we do ar[ar % 2 != 0], we are essentially filtering the original array where the condition evaluates to True.

You can similarly filter a Numpy array for other conditions as well.

Summary – Get all odd elements in a Numpy array

In this tutorial, we looked at how to get the odd elements of a Numpy array. The following is a short summary of the steps mentioned –

  1. Create a Numpy array (skip this step if you already have an array to operate on).
  2. Use boolean indexing to filter the array for only the values that are odd, ar[ar % 2 != 0].

You might also be interested in –


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Piyush is a data scientist passionate about using data to understand things better and make informed decisions. In the past, he's worked as a Data Scientist for ZS and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.