Python iterating over collection with index


Python interation with index example source code;


listIterationWithIndex = ['Test1','Goose','Bison','Pelican']

#iterate over list with index
for i, withIndex in enumerate(listIterationWithIndex):
    print ("element -- ", i , withIndex)

'''
output
element --  0 Test1
element --  1 Goose
element --  2 Bison
element --  3 Pelican
'''

tuple_ =  ('Cat','Dog','Elephant','Bird','Tiger','Lion',\
           'Rabbit')

#iterate over tuple with index
for i, withIndex in enumerate(tuple_):
    print ("element -- ", i , withIndex)

'''
output
element --  0 Cat
element --  1 Dog
element --  2 Elephant
element --  3 Bird
element --  4 Tiger
element --  5 Lion
element --  6 Rabbit
'''
    
dictionaryAnimalSpeed = {'tiger': 65, 'monkey': 25 ,'bison': 35, 'cheetah': 113 }

#iterate over dictionary with index
for i, withIndex in enumerate(dictionaryAnimalSpeed.keys()):
    print ("element -- ", i , withIndex)

'''
output
element --  0 bison
element --  1 cheetah
element --  2 monkey
element --  3 tiger
'''    
    
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s