---
title: nonzero()
url: https://docs.vultr.com/python/third-party/numpy/nonzero
description: Find Non-Zero Elements
publish_date: 2024-11-15T19:40:00.059402Z
last_updated: 2024-11-15T19:40:00.059402Z
header_image: https://assets.vultrdevrel.com/docs-main-doc-assets-1/collection-items/928/54e1ac21-e76e-4fff-8ee8-8e1f570c3718.jpg
---

> Index of every Vultr Docs page in markdown: https://docs.vultr.com/llms.txt

# Python Numpy nonzero() - Find Non-Zero Elements

## Introduction
The `nonzero()` function in Python's NumPy library is a powerful tool for identifying all non-zero elements' indices from an array. This capability is particularly useful for processing matrices or vectors in scientific computing and data analysis, where determining active elements or features is crucial.

In this article, you will learn how to effectively utilize the `nonzero()` function to handle and manipulate arrays in Python. Discover how this function can assist in filtering and processing your data by returning indices of non-zero elements, which can be crucial for condition-based operations.

## Using nonzero() in NumPy Arrays

### Identifying Non-Zero Elements in a One-Dimensional Array
1. Create a one-dimensional NumPy array with both zero and non-zero elements.
1. Apply the `nonzero()` function to find the indices of non-zero elements.

    ```python
    import numpy as np

    array_one_dim = np.array([0, 2, 0, 3, 4, 0])
    non_zero_indices = np.nonzero(array_one_dim)
    print(non_zero_indices)
    ```

    This code snippet creates an array and uses `nonzero()` to find the indices where the elements are non-zero. The output is a tuple indicating the positions of these non-zero elements.

### Handling Multi-Dimensional Arrays
1. Consider a two-dimensional NumPy array.
1. Use the `nonzero()` function to extract indices of non-zero elements in this multi-dimensional context.

    ```python
    array_two_dim = np.array([[0, 1, 0], [4, 0, 6], [0, 0, 0]])
    non_zero_indices = np.nonzero(array_two_dim)
    print(non_zero_indices)
    ```

    In this example, `nonzero()` provides the row and column indices of non-zero elements as separate arrays within a tuple. The result shows which rows and columns to look at for non-zero values.

### Applying Conditions to Arrays
1. Initialize an array of numbers.
1. Apply a condition (e.g., find elements greater than a specific value) and use `nonzero()` to identify qualifying elements' indices.

    ```python
    array_conditions = np.array([1, 2, 3, 4, 5])
    greater_than_two = np.nonzero(array_conditions > 2)
    print(greater_than_two)
    ```

    This segment checks for elements greater than two and utilizes `nonzero()` to index those elements. The output includes the positions of elements that meet the condition.

## Utilizing Results from nonzero()

### Extract Actual Elements Using Indices
1. Start with indices obtained from `nonzero()`.
1. Retrieve actual elements from the original array using these indices.

    ```python
    original_array = np.array([1, 2, 0, 3, 4])
    indices = np.nonzero(original_array)
    elements = original_array[indices]
    print(elements)
    ```

    Leveraging the indices you gathered, extract the non-zero elements from the `original_array`. This method ensures you only work with relevant data, improving efficiency.

## Conclusion
The `nonzero()` function in the NumPy library serves as a fundamental tool for identifying active elements within arrays, facilitating efficient data handling and processing in Python. By mastering the use of `nonzero()`, you streamline analytical processes in scientific computing and gain a superior ability to manipulate and interpret array data based on specified conditions. Whether working with simple lists or complex multi-dimensional arrays, leverage the techniques discussed to optimize your operations and maintain clarity in your data-driven endeavors.