The index method does not do what you expect. To get an item at an index, you must use the [] syntax:

>>> my_list = ['foo', 'bar', 'baz']
>>> my_list[1]  # indices are zero-based
'bar'

index is used to get an index from an item:

>>> my_list.index('baz')
2

If you're asking whether there's any way to get index to recurse into sub-lists, the answer is no, because it would have to return something that you could then pass into [], and [] never goes into sub-lists.

Answer from icktoofay on Stack Overflow
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ numpy โ€บ numpy_array_indexing.asp
NumPy Array Indexing
The second number represents the ... 4 5 6 Since we selected 2, we end up with the third value: 6 ยท Use negative indexing to access an array from the end....
Top answer
1 of 5
15

The index method does not do what you expect. To get an item at an index, you must use the [] syntax:

>>> my_list = ['foo', 'bar', 'baz']
>>> my_list[1]  # indices are zero-based
'bar'

index is used to get an index from an item:

>>> my_list.index('baz')
2

If you're asking whether there's any way to get index to recurse into sub-lists, the answer is no, because it would have to return something that you could then pass into [], and [] never goes into sub-lists.

2 of 5
6

list is an inbuilt function don't use it as variable name it is against the protocol instead use lst.

To access a element from a list use [ ] with index number of that element

lst = [1,2,3,4]
lst[0]
1

one more example of same

lst = [1,2,3,4]
lst[3]
4

Use (:) semicolon to access elements in series first index number before semicolon is Included & Excluded after semicolon

lst[0:3]
[1, 2, 3]

If index number before semicolon is not specified then all the numbers is included till the start of the list with respect to index number after semicolon

lst[:2]
[1, 2]

If index number after semicolon is not specified then all the numbers is included till the end of the list with respect to index number before semicolon

lst[1:]
[2, 3, 4]

If we give one more semicolon the specifield number will be treated as steps

lst[0:4:2]
[1, 3]

This is used to find the specific index number of a element

lst.index(3)
2

This is one of my favourite the pop function it pulls out the element on the bases of index provided more over it also remove that element from the main list

lst.pop(1)
2

Now see the main list the element is removed..:)

lst
[1, 3, 4]

For extracting even numbers from a given list use this, here i am taking new example for better understanding

lst = [1,1,2,3,4,44,45,56]

import numpy as np

lst = np.array(lst)
lst = lst[lst%2==0]
list(lst)
[2, 4, 44, 56]

For extracting odd numbers from a given list use this (Note where i have assingn 1 rather than 0)

lst = [1,1,2,3,4,44,45,56]

import numpy as np

lst = np.array(lst)
lst = lst[lst%2==1]
list(lst)
[1, 1, 3, 45]

Happy Learning...:)

๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-array-indexing
Python Array Indexing - GeeksforGeeks
July 23, 2025 - Python arrays are zero-indexed, just like Lists. First element is at index 0, the second at index 1 and so on.
๐ŸŒ
Hpc-carpentry
hpc-carpentry.org โ€บ hpc-python โ€บ 03-lists โ€บ index.html
Numpy arrays and lists โ€“ Introduction to High-Performance Computing in Python
January 20, 2023 - We add a set of square brackets after the list in question along with the index of the values we want. Note that in Python, all indices start from 0 โ€” the first element is actually the 0th element (this is different from languages like R or MATLAB). The best way to think about array indices is that they are the number of offsets from the first position โ€” the first element does not require an offset to get to.
๐ŸŒ
Quansight-labs
quansight-labs.github.io โ€บ ndindex โ€บ indexing-guide โ€บ index.html
Guide to NumPy Indexing - ndindex documentation
These indices will not work on the built-in Python sequence types like list and str; they are only defined for NumPy arrays. This section is itself split into six subsections. First is a basic introduction to what a NumPy array is. Following this are pages for each of the remaining index types, the basic indices: tuples, ellipses, and newaxis; and the advanced indices: integer arrays and boolean arrays (i.e., masks).
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ indexing a np.array with another np.array
r/learnpython on Reddit: Indexing a np.array with another np.array
July 14, 2022 -

Indexing one array with another array has different behavior than if I index with the same array without explicitly casting it to a numpy array first (i.e. I leave it as a list of lists). I can't find the pages in the documentation that explain this kind of indexing

Example:

  #make a 5x5 matrix for testing, the numbers arent important 
  a = np.random.rand(5,5)

  #another arbitrary 5x5 matrix
  b = [[0, 0, 0, 0, 1],
         [0, 0, 0, 1, 1],
         [0, 0, 1, 1, 0],
         [0, 1, 1, 0, 0],
         [1, 1, 0, 0, 0]]

  c = np.array(b)

  a[b] #gives the error "too many indices for array: array is 2-dimensional, but 5 were indexed"

  a[tuple(c)] #gives the same error as a[b]

  a[c] #for some reason this works, and it returns a 5x5x5 matrix 

So the behavior changes when I convert the list of lists to a numpy array. And I can't really tell what it's doing by looking at the output of a[c]. It seems to be switching the rows around somehow but I'm confused at why it returns five copies of the original matrix. Is there any page in the documentation that describes this type of indexing?

๐ŸŒ
Programiz
programiz.com โ€บ python-programming โ€บ numpy โ€บ array-indexing
Numpy Array Indexing (With Examples)
In NumPy, each element in an array is associated with a number.In NumPy, each element in an array is associated with a number. The number is known as an array index. Let's see an example to demonstrate NumPy array indexing. Array Indexing in NumPy In the above array, 5 is the 3rd element.
Find elsewhere
๐ŸŒ
Python Like You Mean It
pythonlikeyoumeanit.com โ€บ Module3_IntroducingNumpy โ€บ BasicIndexing.html
Introducing Basic and Advanced Indexing โ€” Python Like You Mean It
According to its definition, we must supply our array-indices as a tuple in order to invoke basic indexing. As it turns out, we have been forming tuples of indices all along! That is, every time that we index into an array using the syntax x[i, j, k], we are actually forming a tuple containing those indices.
๐ŸŒ
CodeSignal
codesignal.com โ€บ learn โ€บ courses โ€บ numpy-basics โ€บ lessons โ€บ array-indexing-and-slicing-in-numpy
Array Indexing and Slicing in NumPy - Python
Array indexing lets us access an element in an array. It works just like with Python's lists! Python uses zero-based indexing, meaning the first element is at position 0.
๐ŸŒ
Python Like You Mean It
pythonlikeyoumeanit.com โ€บ Module3_IntroducingNumpy โ€บ AdvancedIndexing.html
Advanced Indexing โ€” Python Like You Mean It
We specify three index-arrays; the indices to be accessed along axis-0, axis-1, and axis-2, respectively. Suppose we want to produce the array: array([ 3, 23, 4]). The layout of these elements is as follows: ... Each index-array must have a shape (3,) in order to produce the result of the ...
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ stable โ€บ user โ€บ basics.indexing.html
Indexing on ndarrays โ€” NumPy v2.4 Manual
The simplest case of indexing with N integers returns an array scalar representing the corresponding item. As in Python, all indices are zero-based: for the i-th index \(n_i\), the valid range is \(0 \le n_i < d_i\) where \(d_i\) is the i-th element of the shape of the array.
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ python-array-tutorial-define-index-methods
Python Array Tutorial โ€“ Define, Index, Methods
January 31, 2022 - If there is more than one element with the same value, the index of the first instance of the value will be returned: import array as arr numbers = arr.array('i',[10,20,30,10,20,30]) #search for the index of the value 10 #will return the index number of the first instance of the value 10 print(numbers.index(10)) #output #0
๐ŸŒ
DataCamp
datacamp.com โ€บ doc โ€บ numpy โ€บ array-indexing
NumPy Array Indexing
NumPy array indexing is used to extract or modify elements in an array based on their indices.
๐ŸŒ
Data-apis
data-apis.org โ€บ array-api โ€บ 2022.12 โ€บ API_specification โ€บ indexing.html
Indexing โ€” Python array API standard 2022.12 documentation
Let N be the number of dimensions (โ€œrankโ€) of a multi-dimensional array A. Each axis may be independently indexed via single-axis indexing by providing a comma-separated sequence (โ€œselection tupleโ€) of single-axis indexing expressions (e.g., A[:, 2:10, :, 5]). ... In Python, A[(exp1, exp2, ..., expN)] is equivalent to A[exp1, exp2, ..., expN]; the latter is syntactic sugar for the former.
๐ŸŒ
NumPy
numpy.org โ€บ devdocs โ€บ user โ€บ basics.indexing.html
Indexing on ndarrays โ€” NumPy v2.5.dev0 Manual
The simplest case of indexing with N integers returns an array scalar representing the corresponding item. As in Python, all indices are zero-based: for the i-th index \(n_i\), the valid range is \(0 \le n_i < d_i\) where \(d_i\) is the i-th element of the shape of the array.
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_arrays.asp
Python Arrays
An array can hold many values under a single name, and you can access the values by referring to an index number.
๐ŸŒ
Python documentation
docs.python.org โ€บ 3 โ€บ tutorial โ€บ datastructures.html
5. Data Structures โ€” Python 3.14.3 documentation
The optional arguments start and end are interpreted as in the slice notation and are used to limit the search to a particular subsequence of the list. The returned index is computed relative to the beginning of the full sequence rather than ...
๐ŸŒ
Pandas
pandas.pydata.org โ€บ docs โ€บ user_guide โ€บ indexing.html
Indexing and selecting data โ€” pandas 3.0.1 documentation
The semantics follow closely Python and NumPy slicing. These are 0-based indexing. When slicing, the start bound is included, while the upper bound is excluded. Trying to use a non-integer, even a valid label will raise an IndexError. The .iloc attribute is the primary access method. The following are valid inputs: An integer e.g. 5. A list or array of integers [4, 3, 0].
๐ŸŒ
Problem Solving with Python
problemsolvingwithpython.com โ€บ 05-NumPy-and-Arrays โ€บ 05.05-Array-Indexing
Array Indexing - Problem Solving with Python
We can access the value 8 in the array above by calling the row and column index [1,2]. This corresponds to the 2nd row (remember row 0 is the first row) and the 3rd column (column 0 is the first column). ... Array indexing is used to access values in an array.
๐ŸŒ
Pluralsight
pluralsight.com โ€บ tech insights & how-to guides โ€บ tech guides & tutorials
Working with Numpy Arrays: Indexing & Slicing | Pluralsight
October 2, 2018 - For example, let me define a one-dimensional array ... Index โ€˜3โ€™ represents the starting element of the slice and it's inclusive. Index โ€˜6โ€™ represents the stopping element of the slice and itโ€™s exclusive.