Index of Non-Zero Elements in Python list Last Updated : 05 Feb, 2025 Comments Improve Suggest changes Like Article Like Report We are given a list we need to find all indexes of Non-Zero elements. For example, a = [0, 3, 0, 5, 8, 0, 2] we need to return all indexes of non-zero elements so that output should be [1, 3, 4, 6].Using List ComprehensionList comprehension can be used to find the indices of non-zero elements by iterating through the list with enumerate(). It collects indices where element is not zero into a new list. Python a = [0, 3, 0, 5, 8, 0, 2] # Use list comprehension with enumerate() to get indices of non-zero elements ind = [i for i, val in enumerate(a) if val != 0] print(ind) Output[1, 3, 4, 6] Explanation:enumerate(a) provides both index and value, and list comprehension filters indices where the value is non-zero.Result is a list of indices of non-zero elements.Using a LoopWe can use a loop with enumerate() to iterate through the list and check for non-zero values. If a value is non-zero we append its index to a separate list. Python a = [0, 3, 0, 5, 8, 0, 2] ind = [] for i in range(len(a)): # If the element is non-zero, append its index to the list if a[i] != 0: ind.append(i) print(ind) Output[1, 3, 4, 6] Using NumPyNumPy provides numpy.nonzero() function which returns indices of non-zero elements in an array. Converting a list to a NumPy array allows efficient retrieval of these indices. Python import numpy as np a = [0, 3, 0, 5, 8, 0, 2] # Use np.nonzero() to get indices of non-zero elements and convert to a list ind = np.nonzero(a)[0].tolist() print(ind) Output[1, 3, 4, 6] Explanation:np.nonzero(a) returns a tuple containing an array of indices where elements in a are non-zero, and [0] extracts the first element (the indices array)..tolist() converts the NumPy array of indices into a regular Python list. Comment More infoAdvertise with us Next Article Index of Non-Zero Elements in Python list M manjeet_04 Follow Improve Article Tags : Python Python list-programs Practice Tags : python Similar Reads Get Index of Multiple List Elements in Python In Python, retrieving the indices of specific elements in a list is a common task that programmers often encounter. There are several methods to achieve this, each with its own advantages and use cases. In this article, we will explore some different approaches to get the index of multiple list elem 3 min read Count the Number of Null Elements in a List in Python In data analysis and data processing, It's important to know about Counting the Number of Null Elements. In this article, we'll explore how to count null elements in a list in Python, along with three simple examples to illustrate the concept. Count the Number of Null Elements in a List in PythonIn 3 min read Python | Indices of Kth element value Sometimes, while working with records, we might have a problem in which we need to find all the indices of elements for a particular value at a particular Kth position of tuple. This seems to be a peculiar problem but while working with many keys in records, we encounter this problem. Let's discuss 4 min read Python List index() - Find Index of Item index() method in Python is a helpful tool when you want to find the position of a specific item in a list. It works by searching through the list from the beginning and returning the index (position) of the first occurrence of the element you're looking for. Example:Pythona = ["cat", "dog", "tiger" 3 min read Get index in the list of objects by attribute in Python In this article, we'll look at how to find the index of an item in a list using an attribute in Python. We'll use the enumerate function to do this. The enumerate() function produces a counter that counts how many times a loop has been iterated. We don't need to import additional libraries to utili 2 min read Like