Algorithm
# Algorithm to Access Index of a List Using for Loop
# 1. Define a list
# Example:
# my_list = [10, 20, 30, 40, 50]
# 2. Use a for loop to iterate through the list
# for index in range(len(my_list)):
# Access the element at the current index
# current_element = my_list[index]
# Print or use the index and corresponding element as needed
# Example:
# print(f"Index: {index}, Element: {current_element}")
# 3. End of the program
Code Examples
#1 Code Example- Using enumerate
Code -
Python Programming
my_list = [21, 44, 35, 11]
for index, val in enumerate(my_list):
print(index, val)
Copy The Code &
Try With Live Editor
Output
1 44
2 35
3 11
#2 Code Example- Start the indexing with non zero value
Code -
Python Programming
my_list = [21, 44, 35, 11]
for index, val in enumerate(my_list, start=1):
print(index, val)
Copy The Code &
Try With Live Editor
Output
2 44
3 35
4 11
#3 Code Example- Without using enumerate()
Code -
Python Programming
my_list = [21, 44, 35, 11]
for index in range(len(my_list)):
value = my_list[index]
print(index, value)
Copy The Code &
Try With Live Editor
Output
1 44
2 35
3 11
Demonstration
Python Programing Example to Access Index of a List Using for Loop-DevsEnv