Table of Contents
The enumerate in python comes in very handy when it comes to looping through a sequence of numbers. This function does not only return each number in the sequence but also the position of that number in the sequence as well. If you’re working with nested lists, this allows you to easily determine the location of an item from within one list to another without any help from outside functions or loops. Read on to find out how the to enumerate function works and what you can do with it! Enumerate in Python is used to iterate over items of any sequence type, such as a string, tuple, or list, and return each item’s index and the item itself. Enumerate function in Python may also be called a ‘for loop’, because it works similarly to the for loop in other programming languages, but with an additional option to use the counter of each iteration.
Syntax of Python enumerate()
enumerate(iterable, startIndex)
Parameters
- Iterable: an object that can be looped.
- StartIndex: (optional) The count will start with the value given in the startIndex for the first item in the loop and increment it for the next item till it reaches the end of the loop.
ReturnValue:
It will give back an iterable object with a count value for each of the elements in the input iterator object.
Enumerate() in Python Example
1: Which of the following data types is immutable in Python?
Each item in the Python Enumerate list has an automatic counter or index provided by the Enumerate method. The initial value of the firstindex will be 0. By using the optional startIndex parameter of enumerate() in python, you may also specify the startindex. Mylist is the list provided to the enumerate in python in the code below. The output of Python’s Enumerate function is shown using the list() function.
The output from enumerate will be in the following manner:
(0, item_1), (1, item_2), (2, item_3), … (n, item_n)
File: python_enumerate.py
mylist = ['A', 'B' ,'C', 'D'] e_list = enumerate(mylist) print(list(e_list))
Output:
[(0, 'A'), (1, 'B'), (2, 'C'), (3, 'D')]
Grab the opportunity to learn Python with Entri! Click Here
UsingEnumerate() on a list with startIndex
The startindex in the example below is 2. The startindex is where the first item’s index will begin.
In the example below, mylist is the list given to enumerate() in python. The list() function is used to display the enumerate output.
mylist = ['A', 'B' ,'C', 'D'] e_list = enumerate(mylist,2) print(list(e_list))
Output:
[(2, 'A'), (3, 'B'), (4, 'C'), (5, 'D')]
Looping Over an Enumerate object
Enumerating over an object with and without startIndex is demonstrated in the example.
Since startIndex is absent from the first for-loop, the index begins at 0.
The index starts at 10 because the second for-startIndex loop’s is set to 10.
Example:
mylist = ['A', 'B' ,'C', 'D'] for i in enumerate(mylist): print(i) print("\n") print("Using startIndex as 10") for i in enumerate(mylist, 10): print(i) print("\n")
Output:
(0, 'A') (1, 'B') (2, 'C') (3, 'D') Using startIndex as 10 (10, 'A') (11, 'B') (12, 'C') (13, 'D')
Enumerating a Tuple
You can use a tuple inside of an enumerate in the example below. The key to each item will start from the startIndex provided if you choose to use one.
The startIndex is 0 by default. As a result, you see key there as 0 for items A and 1 for items B and so forth.
Example:
my_tuple = ("A", "B", "C", "D", "E") for i in enumerate(my_tuple): print(i)
Output:
(0, 'A') (1, 'B') (2, 'C') (3, 'D') (4, 'E')
Learn Coding in your Language! Enroll Here!
Enumerating a String
The string is an array in Python, allowing for looping over it. When a string is provided to enumerate(), the output will display the index and value for each character.
Example:
my_str = "Guru99 " for i in enumerate(my_str): print(i)
Output:
(0, 'G') (1, 'u') (2, 'r') (3, 'u') (4, '9') (5, '9')
Enumerate a dictionary
A dictionary is listed in curly brackets in Python, and the values are stated inside these curly brackets.
A comma separates each key/value pair element in the list. A dictionary can be used inside of an enumerate() and the results are displayed.
my_dict = {"a": "PHP", "b":"JAVA", "c":"PYTHON", "d":"NODEJS"} for i in enumerate(my_dict): print(i)
Output:
(0, 'a') (1, 'b') (2, 'c') (3, 'd')
Advantages of using Enumerate
Enumerate provides the values along with the index and enables you to loop through a list, tuple, dictionary, or string. List.index can be used to obtain the index value in a for-loop (n). List.index(n) will traverse the for-loop twice, making it highly costly. In this situation, enumerating is particularly useful because it provides the index and elements all at once.
Summary
Python comes with a built-in function called enumerate(). Each item in the iterable object has a counter added by the enumerate() function, which returns an enumerate object. You can specify the startIndex, or the counter you want the values to start from when using Python’s enumeration function. To loop through a list, tuple, dictionary, or string, use enumerate. When looping through a list, tuple, dictionary, etc., you may want to know both the index and the value. Enumerate is a great tool for this. Each item in the list is automatically indexed and counted by enumerate() in python. The initial index value will be 0, so. By using the optional startIndex parameter of enumerate, you may also specify the startindex. When a string is provided to enumerate(), the output will display the index and value for each character. If you are interested to learn new coding skills, the Entri app will help you to acquire them very easily. Entri app is following a structural study plan so that the students can learn very easily. If you don’t have a coding background, it won’t be any problem. You can download the Entri app from the google play store and enroll in your favorite course.