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....
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-array-indexing
Python Array Indexing - GeeksforGeeks
July 1, 2026 - Indexing is the most common way to retrieve values from an array. Each element has a position called an index, which allows you to access it directly without traversing the entire array.
๐ŸŒ
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).
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...:)

๐ŸŒ
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.
๐ŸŒ
Kitchin Research Group
kitchingroup.cheme.cmu.edu โ€บ blog โ€บ 2013 โ€บ 02 โ€บ 27 โ€บ Indexing-vectors-and-arrays-in-Python
Indexing vectors and arrays in Python
February 27, 2013 - Python does not have the linear assignment method like Matlab does. You can achieve something like that as follows. We flatten the array to 1D, do the linear assignment, and reshape the result back to the 2D array. c = b.flatten() c[2] = 34 b[:] = c.reshape(b.shape) print b ... The 3d array is like book of 2D matrices. Each page has a 2D matrix on it. think about the indexing like this: (row, column, page)
Find elsewhere
๐ŸŒ
Python Like You Mean It
pythonlikeyoumeanit.com โ€บ Module3_IntroducingNumpy โ€บ BasicIndexing.html
Introducing Basic and Advanced Indexing โ€” Python Like You Mean It
Thus far we have seen that we can access the contents of a NumPy array by specifying an integer or slice-object as an index for each one of its dimensions. Indexing into and slicing along the dimensions of an array are known as basic indexing.
๐ŸŒ
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.
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ stable โ€บ user โ€บ basics.indexing.html
Indexing on ndarrays โ€” NumPy v2.5 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.
๐ŸŒ
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 ...
๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ library โ€บ array.html
array โ€” Efficient arrays of numeric values
Array objects support the ordinary mutable sequence operations of indexing, slicing, concatenation, and multiplication. When using slice assignment, the assigned value must be an array object with the same type code; in all other cases, TypeError is raised.
๐ŸŒ
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.
๐ŸŒ
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.
๐ŸŒ
Railsware
railsware.com โ€บ home โ€บ engineering โ€บ indexing and slicing for lists, tuples, strings, other sequential types in python
Python Indexing and Slicing for Lists, Tuples, Strings, other Sequential Types | Railsware Blog
January 22, 2025 - Here we defined a list of colors. Each item in the list has a value(color name) and an index(its position in the list). Python uses zero-based indexing.
๐ŸŒ
WildLearner
wildlearner.com โ€บ learn โ€บ course โ€บ numpy โ€บ arrays โ€บ array-indexing โ€บ array-indexing
NumPy | WildLearner - Learn to code, and get hired.
Introduction to the course NumPy is a powerful library for the Python programming language that provides support for large, multi-dimensional arrays and matrices of numerical data, as well as a large collection of mathematical functions to operate on these arrays.
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ ref_list_index.asp
Python List index() Method
Python Examples Python Compiler ... Plan Python Interview Q&A Python Training ... The index() method returns the position at the first occurrence of the specified value....
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ python โ€บ python_finding_the_index_list_item.htm
Python Finding the Index of a List Item
lst = [25, 12, 10, -21, 10, 100] print ("lst:", lst) x = lst.index(10) print ("First index of 10:", x)
๐ŸŒ
Medium
medium.com โ€บ @whyamit404 โ€บ basics-of-numpy-array-indexing-9052e6d6b5cf
Basics of NumPy Array Indexing. If you think you need to spend $2,000โ€ฆ | by whyamit404 | Medium
February 9, 2025 - Slicing allows you to grab a portion of your array using the syntax: start:stop:step Hereโ€™s how it works: Start: The index where your slice begins (inclusive).