iterate over a list with access to index and value
0
Contents
This python code example will show you how to get the index and value of list items inside a for loop.
# Define a new list using [] square brackets.
fruits = ['banana', 'apple', 'orange', 'lemon', 'lime',
'strawberry', 'kiwi', 'peach', 'grape', 'pineapple', 'mango']
# iterate over a list with access to index and value
print('=== Iterate and print index and value of items in a list ===')
for key, val in enumerate(fruits):
print(key, ":", val)
# output
=== Iterate and print index and value of items in a list ===
0 : banana
1 : apple
2 : orange
3 : lemon
4 : lime
5 : strawberry
6 : kiwi
7 : peach
8 : grape
9 : pineapple
10 : mango