You can use a list comprehension:
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 Posts:
- if else in a list comprehension
- if else in a list comprehension
- List comprehension on a nested list?
- ‘list’ object has no attribute ‘shape’
- ‘list’ object has no attribute ‘shape’
- numpy-equivalent of list.pop?
- TypeError: only integer arrays with one element can be converted to an index 3
- How to create a numpy array of lists?
- How can I reverse a list in Python?
- Difference between del, remove, and pop on lists
- How can I reverse a list in Python?
- What is the difference between Python’s list methods append and extend?
- Python – TypeError: ‘int’ object is not iterable
- Understanding slice notation
- Understanding slice notation
- TypeError: only integer scalar arrays can be converted to a scalar index with 1D numpy indices array
- TypeError: list indices must be integers or slices, not str
- ImportError: DLL load failed: The specified module could not be found
- ImportError: DLL load failed: The specified module could not be found
- IndexError: list index out of range and python
- Python – TypeError: ‘int’ object is not iterable
- Import Error: No module named numpy
- ValueError: setting an array element with a sequence
- How to convert list to string [duplicate]
- How do I get the number of elements in a list?
- numpy max vs amax vs maximum
- Finding the index of an item in a list
- Finding the index of an item in a list
- Accessing the index in ‘for’ loops?
- What exactly does numpy.exp() do? [closed]
- How to make a flat list out of a list of lists
- Removing duplicates in lists
- What does the list() function do in Python?
- Difference between import numpy and import numpy as np
- List changes unexpectedly after assignment. Why is this and how can I prevent it?
- Why does this iterative list-growing code give IndexError: list assignment index out of range?
- What is the purpose of meshgrid in Python / NumPy?
- why numpy.ndarray is object is not callable in my simple for python loop
- numpy division with RuntimeWarning: invalid value encountered in double_scalars
- TypeError: ‘float’ object is not subscriptable
- Numpy ValueError: setting an array element with a sequence. This message may appear without the existing of a sequence?
- numpy: Invalid value encountered in true_divide
- Is there a short contains function for lists?
- python numpy ValueError: operands could not be broadcast together with shapes
- How to fix ‘Object arrays cannot be loaded when allow_pickle=False’ for imdb.load_data() function?
- How do I create an empty array/matrix in NumPy?
- TypeError: ‘float’ object is not subscriptable
- Most efficient way to find mode in numpy array
- ValueError: Unknown label type: ‘continuous’
- Transpose/Unzip Function (inverse of zip)?
- appending list but error ‘NoneType’ object has no attribute ‘append’
- TypeError: unhashable type: ‘numpy.ndarray’
- How to fix IndexError: invalid index to scalar variable
- Could not install packages due to a “Environment error :[error 13]: permission denied : ‘usr/local/bin/f2py'”
- numpy division with RuntimeWarning: invalid value encountered in double_scalars
- How does numpy.newaxis work and when to use it?
- numpy matrix vector multiplication
- Converting list to numpy array
- Check if something is (not) in a list in Python
- How do I read CSV data into a record array in NumPy?
- Python: finding an element in a list [duplicate]
- How to avoid “RuntimeError: dictionary changed size during iteration” error?
- if/else in a list comprehension
- data type not understood
- How do you do natural logs (e.g. “ln()”) with numpy in Python?
- How do I read CSV data into a record array in NumPy?
- Plotting a 2D heatmap with Matplotlib
- How to normalize a NumPy array to a unit vector?
- How to avoid “RuntimeError: dictionary changed size during iteration” error?
- if/else in a list comprehension
- Should I use np.absolute or np.abs?
- Finding the average of a list
- Writing a list to a file with Python
- What does numpy.random.seed(0) do?
- How do I concatenate two lists in Python?
- ImportError: numpy.core.multiarray failed to import
- How can I randomly select an item from a list?
- Factorial in numpy and scipy
- Print a list in reverse order with range()?
- What does the c underscore expression `c_` do exactly?
- Convert pandas dataframe to NumPy array
- Get unique values from a list in python [duplicate]
- Fastest way to check if a value exists in a list
- ImportError: Missing required dependencies [‘numpy’]
- How can I count the occurrences of a list item?
- Unable to plot Double Bar, Bar plot using pyplot for ndarray
- TypeError: ‘list’ object cannot be interpreted as an integer
- Error: all the input array dimensions except for the concatenation axis must match exactly
- Why is it string.join(list) instead of list.join(string)?
- Why is it string.join(list) instead of list.join(string)?
- How do I concatenate two lists in Python?
- Finding median of list in Python
- ImportError: numpy.core.multiarray failed to import
- How to count the occurrence of certain item in an ndarray?
- Python set to list
- size of NumPy array
- Python List vs. Array – when to use?
- Singular matrix issue with Numpy
- Python Pandas – Missing required dependencies [‘numpy’] 1
- numpy array concatenate: “ValueError: all the input arrays must have same number of dimensions”