Python iterate list with index
In this post, you will learn different ways to iterate a list with index.
A list is a sequence of indexed and ordered values, like a dynamic size array. So that we can store multiple items in a single variable. It is mutable, which means we can change the order of elements in a list. It contains a list of any type of data object with a comma separated and enclosed within a square bracket. The elements in a list are indexed, the first item has an index [0], the second item has index [1] etc. In the development process, we may come to the situation where we need to loop through the list with the index. There are various ways to loop through a list. Here, we have mentioned most of them with a simple example -
Python iterate list using for loop with enumerate type
Python enumerate() function can be utilized to repeat the list in an enhanced way. This method adds a counter to an iterable and returns it (the enumerate object). It makes it simpler to monitor the substance of an iterable article. Subsequently, it diminishes the overhead of keeping a check of the components while the iteration operation.
list_elems = ['chair', 'fan', 'bulb', 'table', 'cooler']
# Iterate over the list
for index,value in enumerate(list_elems):
print((index, value), end=' ')
Output of the above code -
(0, 'chair') (1, 'fan') (2, 'bulb') (3, 'table') (4, 'cooler')
Python iterate list using range() function
We can also use the combination of range() and len() functions to iterate a list with index.
list_elems = ['bread', 'cookies', 'chocolate', 'tea', 'cake']
# getting length of list
length = len(list_elems )
# Iterating the index
# same as 'for i in range(len(list_elems))'
for i in range(length):
print((i, list_elems [i]), end=' ')
Output of the above code -
(0, 'bread') (1, 'cookies') (2, 'chocolate') (3, 'tea') (4, 'cake')
Python iterate list using zip() function
Python's zip() function makes an iterator that will aggregate elements from at least two iterables. It returns a zip object, which is an iterator of tuples where the main thing in each passed iterator is matched together, and afterward the second thing in each passed iterator is combined together and so on.
list1 = ['Biscuit', 'Chocolate', 'Cookies', 'Coffey']
for x in zip(range(len(list1)), list1):
print(x)
Output of the above code -
(0, 'Biscuit')
(1, 'Chocolate')
(2, 'Cookies')
(3, 'Coffey')
Python iterate list using map() function
The map() function applies a function to each item of an iterable list and returns a list of the results. Lambda is an anonymous function that contains any number of arguments, but only one expression. Here, we have used both map() and lambda function to iterate over the list with index.
list_elems = [42, 74, 22, 65, 23, 52]
result = map(lambda x: (x, list_elems[x]), range(len(list_elems)))
print(list(result))
Output of the above code -
[(0, 42), (1, 74), (2, 22), (3, 65), (4, 23), (5, 52)]
Related Articles
range and xrange in Python
2d arrays in Python
splitlines in python
Hollow Diamond Pattern in Python
Python program to sum all the numbers in a list
Difference between tuple and list in Python
Alphabetical order Python
Write a program to read two numbers and print their quotient and remainder in python
ASCII value in Python
Greatest common divisor (GCD) in Python
Python nonlocal keyword
Prime factor Python
casefold in Python
strip function in Python
Convert array to list Python
Remove element from list Python
Convert list to dictionary Python
Python dict inside list
Convert list to string Python
Remove last element from list Python
Convert string to list Python
Python add list to list
Difference between tuple and list in Python
Alphabetical order Python