+

List () in Python

Syntax :

 enumerate (iterable, start = 0)  Parameters:   Iterable:  any object that supports iteration  Start:  the index value from which the counter is to be started, by default it is 0 

# Python program to illustrate
# enumerate function

l1 = [ "eat" , "sleep" , "repeat" ]

s1 = "geek"

 
# create enum objects

 

obj1 = enumerate (l1)

obj2 = enumerate (s1)

 

print " Return type: " , type (obj1)

print list ( enumerate (l1))

 
# change starting index to 2 from 0

print list ( enumerate (s1, 2 ))

Exit:

 Return type: [(0, `eat`), (1,` sleep`), (2, `repeat` )] [(2, `g`), (3,` e`), (4, `e`), (5,` k`)] 

Using enumeration object in loops

# Python program for illustration
# list the function in loops

l1 = [ "eat" , "sleep" , " repeat " ]

  
# printing tuples directly into the object

for < code class = "plain"> ele in enumerate (l1):

print ele

print  
# change index and print separately

for count, ele in enumerate (l1, 100 ):

print count, ele

Exit:

 (0, `eat`) (1, `sleep`) (2,` repeat`) 100 eat 101 sleep 102 repeat 

This article courtesy of Harshit Agrawal . If you are as Python.Engineering and would like to contribute, you can also write an article using contribute.python.engineering or by posting an article contribute @ python.engineering. See my article appearing on the Python.Engineering homepage and help other geeks.

Please post comments if you find anything wrong or if you`d like to share more information on the topic discussed above.

Get Solution for free from DataCamp guru