You can use a list comprehension with enumerate
:
indices = [i for i, x in enumerate(my_list) if x == "whatever"]
The iterator enumerate(my_list)
yields pairs (index, item)
for each item in the list. Using i, x
as loop variable target unpacks these pairs into the index i
and the list item x
. We filter down to all x
that match our criterion, and select the indices i
of these elements.
Related Contents:
- What does “list comprehension” and similar mean? How does it work and how can I use it?
- What are the advantages of NumPy over regular Python lists?
- List comprehension on a nested list?
- Pythonic way to print list items
- if else in a list comprehension [duplicate]
- Transpose a matrix in Python [duplicate]
- Python’s most efficient way to choose longest string in list?
- Why is a list comprehension so much faster than appending to a list?
- List of lists into numpy array
- List comprehension vs generator expression’s weird timeit results?
- How to get the index of a maximum element in a NumPy array along one axis
- How to unzip a list of tuples into individual lists? [duplicate]
- list() uses slightly more memory than list comprehension
- Appending item to lists within a list comprehension
- Find nearest indices for one array against all values in another array – Python / NumPy
- remove None value from a list without removing the 0 value
- How to frame two for loops in list comprehension python
- What is the inverse function of zip in python? [duplicate]
- Convert NumPy array to Python list
- Filtering a list based on a list of booleans
- How to use python numpy.savetxt to write strings and float number to an ASCII file?
- Comprehension on a nested iterables?
- Python Array Slice With Comma?
- python pandas flatten a dataframe to a list
- How to return a subset of a list that matches a condition [duplicate]
- How to remove multiple items from a list in just one statement?
- Python using enumerate inside list comprehension
- How can I use list comprehensions to process a nested list?
- Index all *except* one item in python
- Flattening a list of NumPy arrays?
- Nested list comprehensions
- How to convert list of numpy arrays into single numpy array?
- Why list comprehension is much faster than numpy for multiplying arrays?
- How to create a numpy array of lists?
- list.extend and list comprehension [duplicate]
- List with duplicated values and suffix
- Numpy individual element access slower than for lists
- Skip elements on a condition based in a list comprehension in python
- compare two lists in python and return indices of matched values
- How to access a column in a list of lists in python
- sum each value in a list of tuples
- How to reverse a list?
- Apply function to each element of a list
- Appending to the same list from different processes using multiprocessing
- How does the max() function work on list of strings in python?
- numpy reverse multidimensional array
- Creating a Confidence Ellipsis in a scatterplot using matplotlib
- TypeError: ‘float’ object is not subscriptable
- Correlation of 2 time dependent multidimensional signals (signal vectors)
- Generate random array of floats between a range