Enumerate

Enumerate() Function in Python: Loop, Tuple, String (Example)

What is Python Enumerate and how does it work?
Python Enumerate() is a built-in function that is included with the Python programming language. It accepts as input a collection or a tuple of tuples and returns an enumerate object of the same type. A counter is added to each item of the iterable object using the Python Enumerate() command, which outputs an enumerate object in the form of an output string.

In this Enumerate Python lesson, you will learn how to do the following:

What is Python Enumerate and how does it work?
An Example of the Python Syntax Enumerate()
UsingEnumerate() on a list with startIndex as a starting point
The Enumerate object is looped over and the Tuple is enumerated
Counting the number of characters in a string
Make a list of all the words in a dictionary.
Using Enumerate has a number of advantages.
Python’s enumerate function has the following syntax: ()
enumerate the number of (iterable, startIndex)
Parameters
There are three parameters:

Iterable: an object that can be looped over and over again.
StartIndex:  (optional) The count will begin with the number specified in the startIndex for the first item in the loop and will continue to increase with each subsequent item until it reaches the conclusion of the loop.
Alternatively, if the startIndex parameter is not supplied, the count will begin at 0.

If the iteratorobject provided as input has more than one item, the return value will be an iterableobject with a countvalue for each of the items in the iteratorobject.

Exemplification of the Enumerate() function in Python
When using the Enumerate method in Python, each item in the Enumerate list is automatically assigned a counter/index, which is automatically incremented. The firstindex value will be zero, and so on. Additionally, the startindex can be specified by using the optional argument startIndex in the enumerate function.

Example
In the code below, mylist refers to the list passed to the Enumerate function in the Python programming language. The Enumerate Python result is displayed using the list() method, which is defined as follows:

Note that because there is no startIndex specified, the index for the firstitem will begin at 0.

The following will be the format of the output from enumerate:

The items are numbered from 0 to 1, 1 to 2, 2 to 3, and so on up to (n, item n).
python enumerate.py is the file that was used.

mylist = [‘A’, ‘B’, ‘C’, ‘D’] where A is the first letter of the alphabet.
e list = enumerate(mylist) print(list(e list) e list = enumerate(mylist)
Output:

[(0, ‘A’), (1, ‘B’), (2, ‘C’), (3, ‘D’)] [(0, ‘A’), (1, ‘B’), (2, ‘C’), (3, ‘D’)]
UsingEnumerate() on a list with startIndex as a starting point
Example: In the following example, the startindex is specified as 2.
The index of the firstitem will begin with the startindex that has been specified.

Example:

In the following example, mylist is the list that has been provided to be enumerated. The output of the enumerate function is shown using the list() function.

mylist = [‘A’, ‘B’, ‘C’, ‘D’] where A is the first letter of the alphabet.
I’m going to use e list to enumerate the items in my list and then print the results of list(e list).
Output:

[(2, ‘A’), (3, ‘B’), (4, ‘C’), (5, ‘D’)] [(2, ‘A’), (3, ‘B’), (4, ‘C’), (5, ‘D’)]
Continually iterating over an Enumerate object
The following example demonstrates enumerating over an object with and without a startIndex parameter.

Due to the fact that the initial for-loop does not contain a startIndex, the index starts at 0.
The startIndex of the second for-loop is set to 10, which means that the index begins at number 10.
Example:

mylist = [‘A’, ‘B’, ‘C’, ‘D’] where A is the first letter of the alphabet.

for the integer I in enumerate(mylist):
print(i) print the text (“\n”)

make a printout (“Using startIndex as 10”)

the following is true for I in enumerate(mylist,10):
print(i) print the text (“\n”)
Output:

(0, ‘A’) (1, ‘B’) (0, ‘C’) (0, ‘D’)
(2, letter ‘C’)
(3, letter ‘D’)

using startIndex as 10 (10, ‘A’) (11, ‘B’) (12, ‘C’) and endIndex as 13 (13, ‘D’),
Counting the number of elements in a tuple
Use of a tuple within an enumerate is demonstrated in the following example. You can alternatively specify a startIndex, in which case the key to each item will begin at the startIndex specified.

The startIndex is set to 0 by default. As a result, you will see key as 0 for items A and 1 for items B and so on.

Example:

my tuple = (“A,” “B,” “C,” “D,” “E”) for I in enumerate(my tuple): my tuple = (“A,” “B,” “C,” “D,” “E”) for I in enumerate(my tuple): my tuple = (“A,” “B,” “C,” “D,” “E”) for I in enumerate(my tuple)
print(i) \sOutput:

(0, ‘A’) (1, ‘B’) (0, ‘C’) (0, ‘D’)
(2, ‘C’), (3, ‘D’), and (4, ‘E’) are the letters of the alphabet.
Counting the number of characters in a string
In Python, the string is treated as an array, allowing you to iterate over it. You can get the index and value for each character of a string by passing it to enumerate().

Example:

my str = “Guru99” for I in enumerate(my str): my str = “Guru99” for I in enumerate(my str):
print(i) \sOutput:

(0, the letter ‘G’) (1, the letter ‘u’)
(2, the letter ‘r’)
(3, ‘u’) (4, ‘9’) (5, ‘9’) (3, ‘u’)
Make a list of all the words in a dictionary.
When using Python, a dictionary is listed between curly brackets, and the values are stated within these curly brackets.

Each element consists of a key/value pair that is separated by commas between each other. You can utilise a dictionary within an enumerate() function and observe what happens.

For each I in enumerate(my dict), my dict contains the following values: “a”: “PHP”, “b”: “JAVA”, “c”:”PYTHON”, “d”:”NODEJS” my dict contains the following values:
print(i) \sOutput:

(0, ‘a’) (1, ‘b’) (0, ‘a’)
(2, letter ‘c’)
(3, letter ‘d’)