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?
- 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
- Import Error: No module named numpy
- Accessing the index in ‘for’ loops?
- Difference between import numpy and import numpy as np
- Numpy ValueError: setting an array element with a sequence. This message may appear without the existing of a sequence?
- Is there a short contains function for lists?
- appending list but error ‘NoneType’ object has no attribute ‘append’
- numpy division with RuntimeWarning: invalid value encountered in double_scalars
- Converting list to numpy array
- Check if something is (not) in a list in Python
- 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?
- How to normalize a NumPy array to a unit vector?
- Factorial in numpy and scipy
- TypeError: ‘list’ object cannot be interpreted as an integer
- How to count the occurrence of certain item in an ndarray?
- size of NumPy array
- Python Pandas – Missing required dependencies [‘numpy’] 1
- Get list from pandas dataframe column or row?
- ‘DataFrame’ object has no attribute ‘sort’
- TypeError: cannot unpack non-iterable int objec
- Using Numpy Vectorize on Functions that Return Vectors
- Remove list from list in Python
- How can I compare two lists in python and return matches
- ‘End of statement expected’ in pycharm
- Python: Array v. List
- initialize a numpy array
- How do I check if a list is empty?
- How can I use numpy.correlate to do autocorrelation?
- How to access the ith column of a NumPy multidimensional array?
- Python: find position of element in array
- How to convert a PIL Image into a numpy array?
- Overcome ValueError for empty array
- numpy with python: convert 3d array to 2d
- Python Sets vs Lists
- AttributeError: ‘list’ object has no attribute ‘replace’ when trying to remove character
- Pytorch reshape tensor dimension
- How to remove specific elements in a numpy array
- Add single element to array in numpy
- Overflow / math range error for log or exp
- Creating a Pandas DataFrame from a Numpy array: How do I specify the index column and column headers?
- Overflow Error in Python’s numpy.exp function
- How to check if a string is a substring of items in a list of strings?
- Pop index out of range
- Print list without brackets in a single row
- Dictionary in a numpy array?
- RuntimeWarning: divide by zero encountered in log
- how to create a list of lists
- Python assigning multiple variables to same value? list behavior
- How do I check if there are duplicates in a flat list?
- Append a tuple to a list – what’s the difference between two ways?
- Error NameError: name ‘np’ is not defined
- How to fix “TypeError: len() of unsized object”
- How can I remove all instances of an element from a list in Python?
- Create an empty list in Python with certain size
- How to concatenate items in a list to a single string?
- Numpy.dot TypeError: Cannot cast array data from dtype(‘float64’) to dtype(‘S32’) according to the rule ‘safe’
- NumPy array is not JSON serializable
- What’s the difference between lists enclosed by square brackets and parentheses in Python?
- Why is there no tuple comprehension in Python?
- How to calculate a Gaussian kernel matrix efficiently in numpy?
- Does Numpy automatically detect and use GPU?
- How can I check whether a numpy array is empty or not?
- “Divide by zero encountered in log” when not dividing by zero
- Appending to a list gives ‘int’ object has no attribute ‘append’
- Good ways to “expand” a numpy ndarray?
- Python data structure sort list alphabetically
- TypeError: ‘numpy.float64’ object does not support item assignment
- TypeError: unsupported format string passed to list.__format__
- Official abbreviation for: import scipy as sp/sc
- Create numpy matrix filled with NaNs
- Concat DataFrame Reindexing only valid with uniquely valued Index objects
- Replacing Pandas or Numpy Nan with a None to use with MysqlDB
- threshold in 2D numpy array
- Is it possible to use ‘else’ in a list comprehension?
- Numpy Resize/Rescale Image
- numpy array concatenation error: 0-d arrays can’t be concatenated
- Calculate weighted average using a pandas/dataframe
- RuntimeError: module compiled against API version 0xc but this version of numpy is 0xb
- How to install NumPy for Python 3.6
- Complex number troubles with numpy
- Determine if 2 lists have the same elements, regardless of order? [duplicate]
- Building a list inside a list in python
- Writing Python lists to columns in csv
- Filtering a NumPy Array
- How to install numpy to Python 3.5?
- Function to determine if two numbers are nearly equal when rounded to n significant decimal digits
- TypeError: zip argument #2 must support iteration
- CS231n: How to calculate gradient for Softmax loss function?
- How to zip two 1d numpy array to 2d numpy array [duplicate]
- numpy/scipy/ipython:Failed to interpret file as a pickle