Write a Python Program to Access Index of a List Using for Loop

Python Program to Access Index of a List Using for Loop

Welcome to the World of Online Learning:

Hello Friends “This blog helps you to learn  Python programming concepts. You can learn Python language at your own speed and time. One can learn concepts of Python language by practicing various programs given on various pages of this blog. Enjoy the power of Self-learning using the Internet.”

Python Program to Access Index of a List Using for Loop
Python Program to Access Index of a List Using for Loop

Write a Python Program to Access Index of a List Using for Loop

PROGRAM: Python Program to Access Index of a List Using for Loop

/* Python Program to Access Index of a List Using for Loop */

1: Using enumerate

my_list = [21, 44, 35, 11]

for index, val in enumerate(my_list):
print(index, val)

OUTPUT:-

0 21
1 44
2 35
3 11

2: Start the indexing with non zero value

my_list = [21, 44, 35, 11]

for index, val in enumerate(my_list, start=1):
print(index, val)

OUTPUT:-

1 21
2 44
3 35
4 11

3: Without using enumerate()

my_list = [21, 44, 35, 11]

for index in range(len(my_list)):
value = my_list[index]
print(index, value)

OUTPUT:-

0 21
1 44
2 35
3 11

Leave a Reply

Your email address will not be published. Required fields are marked *