Sometimes, while working with python list, we can have a problem in which we need to find positions of all the integers other than 0. This can have application in day-day programming or competitive programming. Let’s discuss a shorthand by which we can perform this particular task.
Method : Using
enumerate() + list comprehension
This method can be performed using combination of functionalities. In this, we use enumerate function to access index-element together and list comprehension is used for iteration and logic creation.
The original list is : [6, 7, 0, 1, 0, 2, 0, 12] Indices of Non-Zero elements : [0, 1, 3, 5, 7]
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- Python | Pandas Series.nonzero() to get Index of all non zero values in a series
- numpy.nonzero() in Python
- Python | Numpy matrix.nonzero()
- Numpy recarray.nonzero() function | Python
- numpy.ma.MaskedArray.nonzero() function - Python
- Python | Replace elements in second list with index of same element in first list
- Python | Add list elements with a multi-list based on index
- Python - Replace index elements with elements in Other List
- Python - Check if elements index are equal for list elements
- Python | Sort list of list by specified index
- Python - Sort dictionaries list by Key's Value list index
- Python - Filter the List of String whose index in second List contaons the given Substring
- Python | Equate two list index elements
- Python - Product of elements using Index list
- Python - Extract Kth index elements from Dictionary Value list
- Python - Elements Maximum till current index in List
- Python - K Maximum elements with Index in List
- Dunn index and DB index - Cluster Validity indices | Set 1
- Python | Convert list of string to list of list
- Python | Convert list of tuples to list of list
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.