Back to Python
2026-03-175 min read

Python Program to Access Index of a List Using for Loop

Learn Python Program to Access Index of a List Using for Loop step by step with clear examples and exercises.

Why This Matters

Understanding how to access the index of a list using a for loop in Python is crucial for working with data structures in Python. It helps you understand the relationship between the elements and their positions within the list, which is essential when writing complex programs that manipulate or analyze data. In this lesson, we'll explore various methods to access the index of a list using a for loop, including the built-in enumerate() function and the range() function.

Prerequisites

Before diving into the core concept, make sure you have a good understanding of the following topics:

  • Basic Python Syntax
  • Data Structures (Lists)
  • Variables and Assignment
  • Control Flow Statements (Loops and Conditional Statements)

Core Concept

Using enumerate() function

To access the index of a list using a for loop in Python, you can use the built-in enumerate() function. This function allows you to loop through both the indices and values of a list simultaneously. Here's an example:

my_list = [21, 44, 35, 11]
for index, val in enumerate(my_list):
print(index, val)

In this code snippet, we have a list called my_list. We then use the enumerate() function to loop through the list. The enumerate() function returns an enumerated sequence that consists of tuples containing the index and value for each item in the iterable (in our case, the list).

Inside the for loop, we have two variables: index and val. The index variable holds the current position of the element within the list, and the val variable holds the actual value at that position. We print both the index and value for each iteration.

You can also start the indexing from a non-zero value by providing the start parameter to the enumerate() function:

my_list = [21, 44, 35, 11]
for index, val in enumerate(my_list, start=1):
print(index, val)

In this example, we started the indexing from 1 instead of 0.

Using range() function and list length

If you prefer not to use enumerate(), you can also access the index directly using the range() function and the length of the list:

my_list = [21, 44, 35, 11]
for index in range(len(my_list)):
value = my_list[index]
print(index, value)

In this example, we're using the range() function to generate a sequence of indices from 0 up to (but not including) the length of the list. For each index, we access the corresponding element in the list using square brackets [].

Worked Example

Let's consider a simple example where we have a list of fruits and want to print both the indices and the fruits:

fruits = ['apple', 'banana', 'cherry', 'date']
for index, fruit in enumerate(fruits):
print(index, fruit)

Output:

0 apple
1 banana
2 cherry
3 date

Common Mistakes

  1. Forgetting to initialize the loop variable: Make sure you initialize both index and val or value when using enumerate(). If you forget to initialize val, Python will throw a NameError since it hasn't been defined yet.
  1. Not understanding the difference between index and value: Sometimes, developers might confuse the index with the value itself. Remember that the index is just a position within the list, while the value is the actual data stored at that position.
  1. Using enumerate() on an empty list: If you try to use enumerate() on an empty list, Python will throw a TypeError since enumerate() expects an iterable as its argument.
  1. Accessing an out-of-range index: If you try to access an index that is greater than or equal to the length of the list, Python will throw an IndexError. To avoid this, make sure you check the index within the bounds of the list before trying to access it.

Practice Questions

  1. Write a program that finds the index of the first occurrence of the number 5 in the following list: [3, 5, 7, 9, 5, 2]
  1. Given the following list of strings, write a program that prints all indices where the string contains the letter 'a': ['cat', 'banana', 'apple', 'orange']
  1. Write a program to find the second largest number in the list [10, 20, 8, 5, 15, 25, 40, 7].
  1. Write a program that reverses the order of elements in a given list using a for loop and without using built-in functions like reverse().

FAQ

How do I access the last element of a list using a for loop?

To access the last element of a list using a for loop, you can use the len() function to get the length of the list and then subtract 1 from it. Here's an example:

my_list = [21, 44, 35, 11]
last_index = len(my_list) - 1
value = my_list[last_index]
print(last_index, value)

Can I use enumerate() with other data structures like tuples or dictionaries?

Yes, you can use the enumerate() function with other data structures like tuples and dictionaries. However, keep in mind that tuples are immutable, so you cannot modify their indices, while dictionaries do not have a specific order for their keys.

What happens if I try to access an index that is out of range?

If you try to access an index that is out of range (i.e., the index is greater than or equal to the length of the list), Python will throw an IndexError. To avoid this, make sure you check the index within the bounds of the list before trying to access it.

How can I sort a list in ascending order using a for loop?

To sort a list in ascending order using a for loop, you can use the built-in sort() function:

my_list = [5, 2, 9, 1, 4]
for i in range(len(my_list)):
for j in range(i+1, len(my_list)):
if my_list[i] > my_list[j]:
my_list[i], my_list[j] = my_list[j], my_list[i]
my_list.sort() # Uncomment this line to use the built-in sort function
print(my_list)

In this example, we're using nested for loops to swap elements if they are in the wrong order. After that, you can uncomment the sort() function to use the built-in sorting function for better performance.

Python Program to Access Index of a List Using for Loop | Python | XQA Learn