The enumerate function takes an iterable like a list, tuple, etc., and returns it as an enumerate object.
The function adds counter to the iterable. For example:
The general way of using the enumerate function is:
Parameters | Description |
iterable | The iterable can be an iterator, sequence, or an object that supports iteration. |
start | The default value of the optional argument start is 0. It defines the starting number of the enumerable object. |
An example of enumerate function with a list
- In this example, we have a list of fruits.
- Then I used the enumerate function and assigned its return value to a variable.
- Finally, the list function is used to display the content of that object variable.
#Enumerate function demo FruitsLst = ['Apple', 'Banana', 'Strawberry', 'Orange'] FruitEnu = enumerate(FruitsLst) # Display counter with elements print(list(FruitEnu))
The output:
You can see, it displayed list of tuple with counter and list elements. As start argument was not provided, so the counter value started at 0.
The next example shows starting the counter value from 10.
Using the start argument in enumerate function
The example below uses the second argument as well in the enumerate function. As mentioned earlier, this is the start value (a number) that defaults to zero. In this example, I used 10:
#Enumerate function demo with start argument FruitsLst = ['Orange', 'Pine Apple', 'Apricot', 'Avocado', 'Cantaloupe'] FruitEnu = enumerate(FruitsLst,10) # Display counter with elements from 10 print(list(FruitEnu))
The result:
So, what is the usefulness of enumerate function?
- It enables us to loop over something
- An automatic counter is added
- The ability to change the start number makes it even more useful.
- By using list function, you may create a tuple of index and list items.
A few more examples of the enumerate function are given below.
Knowing the return type example
By using the type function, you may get the type of returned object after using the enumerate function:
FruitsLst = ['Cherimoya', 'Cherries', 'Clementine'] FruitEnu = enumerate(FruitsLst,15) # Display the type print(type(FruitEnu))
The output:
Using enumerate function in for loop example
In this example, we have an enumerate object and iterate it by using a Python for loop.
The counter and list elements are displayed as follows:
LstNum = [5,7,9,11,13,15] for x, cur_num in enumerate(LstNum): print (x, cur_num)
The output:
Iterating with different start number example
The start argument is also given in this example:
#Enumerate object iteration FruitsLst = ['Orange', 'Pine Apple', 'Apricot', 'Avocado', 'Cantaloupe'] for x,fruit in enumerate(FruitsLst,50): print (x, fruit)
Enumerating a tuple example
In this example, the enumerate function is used with the tuple. Again, a for loop is used to display the tuple items and count/index:
#Enumerate with tuple NameTup = ('Mike', 'Saleem', 'Benjaman', 'Marie', 'Sheela') for count_in,name in enumerate(NameTup): print (count_in, name)
The output:
0 Mike
1 Saleem
2 Benjaman
3 Marie
4 Sheela
Using enumerate function with a string
You may also use enumerate function with the string and get the count added. See an example below:
#Enumerate with String str_x = "Python" for count_in,st in enumerate(str_x): print (count_in,"\t", st)
The output:
0 P
1 y
2 t
3 h
4 o
5 n