Get Index of Item in 2D List in Python (2 Examples)
Hi! This short tutorial will demonstrate how to obtain the index of items in a 2D list in the Python programming language.
Here is a quick overview:
Let’s dive into the Python code!
Create Sample 2D List
We will create the sample 2D list of integers whose items’ indices we will find in this tutorial. So, in your preferred Python IDE, run the line of code below:
my_list = [[1, 2, 3], [4, 5, 6]] print(my_list) # [[1, 2, 3], [4, 5, 6]] print(type(my_list)) # <class 'list'>
We can rearrange the 2D list into rows and columns with a for loop so that it is easier to see its structure:
for items in my_list: print(items) # [1, 2, 3] # [4, 5, 6]
From the printed output, we can see that the 2D list contains 2 rows and 3 columns.
So now that we have created the sample 2D list and can easily see its structure, we will examine 2 different ways to get the index of its items.
Example 1: Obtain Index of Items in 2D List Using Nested For Loop
In this first example, we will use a nested for loop to get the index of the items in the 2D list:
for i in range(len(my_list)): for j in range(len(my_list[i])): print("Index of", my_list[i][j], "is", (i, j)) # Index of 1 is (0, 0) # Index of 2 is (0, 1) # Index of 3 is (0, 2) # Index of 4 is (1, 0) # Index of 5 is (1, 1) # Index of 6 is (1, 2)
The outer loop, using the range() function and the len() function, returns the index positions of the rows, ranging from 0 to 1.
The inner loop also uses the same functions to return the index positions of the columns in the 2D list, ranging from 0 to 2. Remember, Python indexing starts from 0.
The printed results, therefore, tell us the row index and the column index of each integer item in the 2D list. You can examine the results against the structure of the list in the previous section. That way, it makes complete sense.
Example 2: Obtain Index of Items in 2D List Using NumPy
For this example, we will need to install and import Python’s NumPy Library, which is the foremost library for numerical and scientific computations in Python:
# install NumPy pip install numpy # import NumPy import numpy as np
With NumPy installed and imported, we can now use its np.array() function and np.ndenumerate() function to return the index positions of the items in the 2D list:
arr = np.array(my_list) for index, item in np.ndenumerate(arr): print("Index of", item, "is", index) # Index of 1 is (0, 0) # Index of 2 is (0, 1) # Index of 3 is (0, 2) # Index of 4 is (1, 0) # Index of 5 is (1, 1) # Index of 6 is (1, 2)
In the example above, we first converted the 2D list to a NumPy array using the np.array()
function; thereafter, we used a for loop to iterate through and print the array coordinates generated by the np.ndenumerate()
function, which correspond to the row and column indices of the items.
As suggested in the previous example, you can also examine the printed results against the 2D list structure. That way, it is easy to understand and interpret the results.
With that, we have demonstrated how to get the index of items in a 2D list in Python. I do hope you found this tutorial helpful!
Video, Further Resources & Summary
Do you need more explanations on how to get the index of items in a 2D list in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.
In the video, we explain in some more detail how to get the index of items in a 2D list in Python.
The YouTube video will be added soon.
Furthermore, I encourage you to check out other interesting Python list tutorials on Statistics Globe, starting with these:
- count() Method for Lists in Python (2 Examples)
- List Comprehension in Python (Example)
- Convert List of Tuples to List of Lists in Python (3 Examples)
- Learn Python Programming
- Convert List to Matrix & Vice-Versa in Python (Examples)
This post has shown how to get the index of items in a 2D list in Python. In case you have further questions, you may leave a comment below.
This page was created in collaboration with Ifeanyi Idiaye. You might check out Ifeanyi’s personal author page to read more about his academic background and the other articles he has written for the Statistics Globe website.
Statistics Globe Newsletter