python for loop indexing

  • Last Update :
  • Techknowledgy :

Use the built-in function enumerate():

for idx, x in enumerate(xs):
   print(idx, x)

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

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

And note that Python's indexes start at zero, so you would get 0 to 4 with the above. If you want the count, 1 to 5, do this:

count = 0 # in
   case items is empty and you need it after the loop
for count, item in enumerate(items, start = 1):
   print(count, item)
3._
index = 0 # Python 's indexing starts at zero
for item in items: # Python 's for loops are a "for each" loop 
print(index, item)
index += 1
5._
for index in range(len(items)):
   print(index, items[index])

Python's enumerate function reduces the visual clutter by hiding the accounting for the indexes, and encapsulating the iterable into another iterable (an enumerate object) that yields a two-item tuple of the index and the item that the original iterable would provide. That looks like this:

for index, item in enumerate(items, start = 0): #
default is zero
print(index, item)
index = 0 # Python 's indexing starts at zero
for item in items: # Python 's for loops are a "for each" loop 
print(index, item)
index += 1
index = 0
while index < len(items):
   print(index, items[index])
index += 1
for index in range(len(items)):
   print(index, items[index])

It's pretty simple to start it from 1 other than 0:

for index, item in enumerate(iterable, start = 1):
   print index, item # Used to print in python < 3. x
print(index, item) # Migrate to print() after 3. x +

Suggestion : 2

Apr 25th, 2016 9:00 am | Comments , Posted by Trey Hunner Apr 25th, 2016 9:00 am favorite, python

1
2
3
4
var colors = ["red", "green", "blue", "purple"];
for (var i = 0; i < colors.length; i++) {
   console.log(colors[i]);
}
1
2
3
4
5
colors = ["red", "green", "blue", "purple"]
i = 0
while i < len(colors):
   print(colors[i])
i += 1
1
2
3
colors = ["red", "green", "blue", "purple"]
for i in range(len(colors)):
   print(colors[i])

Suggestion : 3

Last Updated : 21 Jun, 2022,GATE CS 2021 Syllabus

Output:

Indices and Index value in the list:
   0 G
1 E
2 E
3 K
4 F
5 O
6 R
7 G
8 E
9 E
10 K
11 S

Suggestion : 4

Using a for loop, iterate through the length of my_list. Loop variable index starts from 0 in this case.,Pass two loop variables index and val in the for loop. You can give any name to these variables.,In this example, you will learn to access the index of a list using a for loop.,In each iteration, get the value of the list at the current index using the statement value = my_list[index].

Example 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

Example 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)

Suggestion : 5

To access index in Python For Loop, you can use enumerate() function or range() function.,In this tutorial, we will go through example programs that demonstrate how to iterate over an iterable and access index as well in the loop.,In the following example, we have used enumerate() function, to access both current index and element during an iteration with for loop.,Please note that, using range() function, to get index and accessing element using index, is not in the spirit of python. enumerate() is relatively recommended if you would like to access index in the for loop.

Python Program

list_1 = ["apple", "banana", "orange", "mango"]

for index, element in enumerate(list_1):
   print(index, element)

Output

0 apple
1 banana
2 orange
3 mango

Suggestion : 6

To access the index in a for loop in Python, use the enumerate() function.,The most elegant way to access the index of for loop in Python is by using the built-in enumerate() function.,Now you understand how to use the enumerate() function to get the index of a for loop in Python.,Now that you understand how the enumerate() function works, let’s use it to access the index in a for loop.

1._
names = ["Alice", "Bob", "Charlie"]

for position, name in enumerate(names):
   print(f "{name}: {position}")

Output:

Alice: 0
Bob: 1
Charlie: 2

For example:

names = ["Alice", "Bob", "Charlie"]
names_idx = enumerate(names)

print(list(names_idx))

For instance:

names = ["Alice", "Bob", "Charlie"]

for index, name in enumerate(names):
   print(f "{name}: {index}")

For example, let’s make indexing start at 1:

names = ["Alice", "Bob", "Charlie"]

for index, name in enumerate(names, start = 1):
   print(f "{name}: {index}")

Suggestion : 7

If you've used another programming language before, you've probably used indexes while looping. Often when you're trying to loop with indexes in Python, you'll find that you actually care about counting upward as you're looping, not actual indexes. ,Unlike, JavaScript, C, Java, and many other programming languages we don't have traditional C-style for loops. Our for loops in Python don't have indexes.,I wouldn't recommend doing this because the official Python style guide, PEP 8, recommends against it and instead of using range of len, there is usually one of two techniques you'll want to use. ,Instead of keeping track of counter ourselves, how about thinking in terms of indices and using range(len(favorite_fruits)) to grab each of the indexes of all the items in this list:

>>> favorite_fruits = ["jujube", "pear", "watermelon", "apple", "blueberry"]
>>> n = 1 >>>
   for fruit in favorite_fruits:
   ...print(n, fruit)
   ...n += 1
   ...
   1 jujube
2 pear
3 watermelon
4 apple
5 blueberry
>>> favorite_fruits = ["jujube", "pear", "watermelon", "apple", "blueberry"] >>>
   >>>
   for i in range(len(favorite_fruits)):
   ...print(i + 1, favorite_fruits[i])
   ...
   1 jujube
2 pear
3 watermelon
4 apple
5 blueberry
>>> favorite_fruits = ["jujube", "pear", "watermelon", "apple", "blueberry"] >>>
   >>>
   for fruit in favorite_fruits:
   ...print(fruit)
   ...
   jujube
pear
watermelon
apple
blueberry
>>> favorite_fruits = ["jujube", "pear", "watermelon", "apple", "blueberry"] >>>
   >>>
   for fruit in enumerate(favorite_fruits):
   ...print(fruit)
   ...
   (0, 'jujube')
   (1, 'pear')
   (2, 'watermelon')
   (3, 'apple')
   (4, 'blueberry')
>>> favorite_fruits = ["jujube", "pear", "watermelon", "apple", "blueberry"] >>>
   >>>
   for fruit in enumerate(favorite_fruits, start = 1):
   ...print(fruit)
   ...
   (1, 'jujube')
   (2, 'pear')
   (3, 'watermelon')
   (4, 'apple')
   (5, 'blueberry')