enumerate(iterator,start) get enumerate object
iterator: Any iterator object or sequence of numbers
start: (optional) Starting number , default value is 0
Example of iterable objects are : List, tuple, dictionary , string
enumerate method ads counter to any iterable object. Each elements of the input object gets an unique incremental number for all its elements.
We will use a list
my_list=['One','Two','Three','Four']
en=enumerate(my_list)
print(list(en))
Output
[(0, 'One'), (1, 'Two'), (2, 'Three'), (3, 'Four')]
Using index and value of a list.
my_list=['Alex','Ronald','John']
for i, my_name in enumerate(my_list):
print('Name #{}: {}'.format(i + 1, my_name))
Output
Name #1: Alex
Name #2: Ronald
Name #3: John
With start
We can set starting number to any value, by default starting is 0. We used a tuple()
my_list=('One','Two','Three','Four')
en=enumerate(my_list,100)
print(list(en))
Output
[(100, 'One'), (101, 'Two'), (102, 'Three'), (103, 'Four')]
Let us use one string
my_str='plus2net'
en=enumerate(my_str)
print(list(en))
Output
[(0, 'p'), (1, 'l'), (2, 'u'), (3, 's'), (4, '2'), (5, 'n'), (6, 'e'), (7, 't')]
Checking data type
We will use type() to get the data type
my_list=['One','Two','Three','Four']
en=enumerate(my_list)
print(type(en)) # <class 'enumerate'>
«All Built in Functions in Python
interator generator
float()
← Subscribe to our YouTube Channel here