Accessing the index in for loops

0 votes
ints = [8, 23, 45, 12, 78]

When I loop through it using a for loop, how can I access the loop index, from 1 to 5 in this case?
Mar 13, 2019 in Python by Mukesh
638 views

1 answer to this question.

0 votes
Use enumerate to get the index with the element as you iterate:

for index, item in enumerate(items):
    print(index, item)

This way you can generate index for your array starting from 1.

Generally the index in Python starts from 0.
answered Mar 13, 2019 by Trisha

Related Questions In Python

+1 vote
1 answer

Accessing the index in 'for' loops?

Using an additional state variable, such as ...READ MORE

answered Aug 28, 2018 in Python by Priyaj
• 58,020 points
700 views
0 votes
1 answer

Accessing the index in 'for' loops?

Using an additional state variable, such as ...READ MORE

answered Dec 16, 2020 in Python by Gitika
• 65,770 points
720 views
0 votes
1 answer

How can I find out the index of an element from row and column in Python?

You probably want to use np.ravel_multi_index: [code] import numpy ...READ MORE

answered Apr 16, 2018 in Python by charlie_brown
• 7,720 points
2,339 views
0 votes
1 answer

Get index in for loop

Use enumerate(): for index, value in enumerate(array): ...READ MORE

answered Apr 24, 2018 in Python by Nietzsche's daemon
• 4,260 points
544 views
0 votes
2 answers

Finding the index of a character in python string

You can use word.find('o') as well to ...READ MORE

answered Jun 1, 2018 in Python by george
• 200 points
1,558 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,434 views
0 votes
1 answer
+5 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,790 points
4,070 views
0 votes
1 answer

How to merge two dictionaries in a single expression?

In Python 3.5 or greater: z = {**x, ...READ MORE

answered Mar 13, 2019 in Python by Trisha
695 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP