An enumerator built-in-function adds a counter of iterable numbers to the provided data structure of integers, characters or strings and many more. Now the data structure might be any list, tuple, dictionary or sets. If the counter is not provided by the user, then it starts from 0 by default. Based on the number provided the enumerator function iterates.
The parameters of the enumerator function are iterable and start.
*Note: The iterable must be an object that supports iteration.*
If you are curious to know the actual syntax of the enumerate function just type help(enumerate) on your IDE and then you will get the following result. Because you need to know this stuff.
help(enumerate)
Help on class enumerate in module builtins: class enumerate(object) | enumerate(iterable[, start]) -> iterator for index, value of iterable | | Return an enumerate object. iterable must be another object that supports | iteration. The enumerate object yields pairs containing a count (from | start, which defaults to zero) and a value yielded by the iterable argument. | enumerate is useful for obtaining an indexed list: | (0, seq[0]), (1, seq[1]), (2, seq[2]), ... | | Methods defined here: | | __getattribute__(self, name, /) | Return getattr(self, name). | | __iter__(self, /) | Implement iter(self). | | __new__(*args, **kwargs) from builtins.type | Create and return a new object. See help(type) for accurate signature. | | __next__(self, /) | Implement next(self). | | __reduce__(...) | Return state information for pickling.
The return type of an enumerate function is an object type. So the enumerate function returns an object by adding the iterating counter value to it. You can also convert the enumerator object into a list(), tuple(), set() and many more.
Let us see an example and understand the enumerate function.
programmming = ["Python", "Programmming", "Is", "Fun"]
print(type(programmming))
enum = enumerate(programmming)
print(type(enum))
<class 'list'> <class 'enumerate'>
#Converting to a list
print(list(enum))
[(0, 'Python'), (1, 'Programmming'), (2, 'Is'), (3, 'Fun')]
#Converting to a tuple
print(tuple(enum))
((0, 'Python'), (1, 'Programmming'), (2, 'Is'), (3, 'Fun'))
#Converting to a set
print(set(enum))
{(3, 'Fun'), (0, 'Python'), (2, 'Is'), (1, 'Programmming')}
When you run the program you will get the following output:
<class 'list'>
<class 'enumerate'>
[(0, 'Python'), (1, 'Programmming'), (2, 'Is'), (3, 'Fun')]
((0, 'Python'), (1, 'Programmming'), (2, 'Is'), (3, 'Fun'))
{(3, 'Fun'), (2, 'Is'), (0, 'Python'), (1, 'Programmming')}
Enumerate function also accepts optional arguments, you can also pass the name of the data structure along with the specific index which you want to start the counter from. For example, think that the default value of the list starts from 0 and then the counter continues to iterate. Now you want to start the counter from 5 and then increment it. This can be done as shown below:
programmming = ["Python", "Programmming", "Is", "Fun"]
# Counter value starts from 0
enum = enumerate(programmming)
print(list(enum))
[(0, 'Python'), (1, 'Programmming'), (2, 'Is'), (3, 'Fun')]
# Counter value starts from 5
enum = enumerate(programmming, 5)
print(list(enum))
[(5, 'Python'), (6, 'Programmming'), (7, 'Is'), (8, 'Fun')]
When you run the program you will get the following output:
[(0, 'Python'), (1, 'Programmming'), (2, 'Is'), (3, 'Fun')]
[(5, 'Python'), (6, 'Programmming'), (7, 'Is'), (8, 'Fun')]
Let us see how can we loop over an enumerate as shown below:
programmming = ["Python", "Programmming", "Is", "Fun"]
for i in enumerate(programmming):
print(i)
for i in enumerate(programmming, 10):
print(i)
(0, 'Python') (1, 'Programmming') (2, 'Is') (3, 'Fun') (10, 'Python') (11, 'Programmming') (12, 'Is') (13, 'Fun')
When you run the program the output will be:
(0, 'Python')
(1, 'Programmming')
(2, 'Is')
(3, 'Fun')
(10, 'Python')
(11, 'Programmming')
(12, 'Is')
(13, 'Fun')
This is the end of the tutorial about *“Python enumerate() built-in-function”*, this is a very short tutorial because this concept is very small and it is not much you can do with it. It is really important to know the concept of Enumerate function so that you can apply this concept elsewhere. To read the official documentation of the enumerate function please go through the link below:
Alright time to say Goodbye, Stay tuned to read more python tutorials. I hope you enjoyed reading this tutorial. Have a good day!