You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Enumerate is built-in python function that takes input as iterator, list etc and returns a tuple containing index and data at that index in the iterator sequence. For example, enumerate(cars), returns a iterator that will return (0, cars[0]), (1, cars[1]), (2, cars[2]), and so on.
# Accessing items using enumerate()
cars = ["Aston" , "Audi", "McLaren "]
for i, x in enumerate(cars):
print (x)
Output :
Aston
Audi
McLaren
Below solution also works.
# Accessing items and indexes enumerate()
cars = ["Aston" , "Audi", "McLaren "]
for x in enumerate(cars):
print (x[0], x[1])
Output :
(0, 'Aston')
(1, 'Audi')
(2, 'McLaren ')
We can also directly print returned value of enumerate() to see what it returns.
# Printing return value of enumerate()
cars = ["Aston" , "Audi", "McLaren "]
print enumerate(cars)
Output :
[(0, 'Aston'), (1, 'Audi'), (2, 'McLaren ')]
Enumerate takes parameter start which is default set to zero. We can change this parameter to any value we like. In the below code we have used start as 1.
# demonstrating use of start in enumerate
cars = ["Aston" , "Audi", "McLaren "]
for x in enumerate(cars, start=1):
print (x[0], x[1])
Output :
(1, 'Aston')
(2, 'Audi')
(3, 'McLaren ')
enumerate() helps to embed solution for accessing each data item in iterator and fetching index of each data item.
In this case, a list and dictionary are to be used for each iteration in a single looping block using enumerate function.
# Two separate lists
cars = ["Aston", "Audi", "McLaren"]
accessories = ["GPS kit", "Car repair-tool kit"]
# Single dictionary holds prices of cars and
# its accessories.
# First two items store prices of cars and
# next three items store prices of accessories.
prices = {1:"570000$", 2:"68000$", 3:"450000$",
4:"890000$", 5:"4500$"}
# Printing prices of cars
for index, c in enumerate(cars, start=1):
print "Car: %s Price: %s"%(c, prices[index])
# Printing prices of accessories
for index, a in enumerate(accessories,start=1):
print ("Accessory: %s Price: %s"\
%(a,prices[index+len(cars)]))
Output:
Car: Aston Price: 570000$
Car: Audi Price: 68000$
Car: McLaren Price: 450000$
Accessory: GPS kit Price: 890000$
Accessory: Car repair-tool kit Price: 4500$
zip function (Both iterators to be used in single looping construct)
This function is helpful to combine similar type iterators(list-list or dict- dict etc) data items at ith position.
It uses shortest length of these input iterators. Other items of larger length iterators are skipped. In case of empty iterators it returns No output.
For example, the use of zip for two lists (iterators) helped to combine a single car and its required accessory.
# Python program to demonstrate working of zip
# Two separate lists
cars = ["Aston", "Audi", "McLaren"]
accessories = ["GPS", "Car Repair Kit",
"Dolby sound kit"]
# Combining lists and printing
for c, a in zip(cars, accessories):
print "Car: %s, Accessory required: %s"\
%(c, a)
The reverse of getting iterators from zip function is known as unzipping using “*” operator.
Use of enumerate function and zip function helps to achieve an effective extension of iteration logic in python and solves many more sub-problems of a huge task or problem.
# Python program to demonstrate unzip (reverse
# of zip)using * with zip function
# Unzip lists
l1,l2 = zip(*[('Aston', 'GPS'),
('Audi', 'Car Repair'),
('McLaren', 'Dolby sound kit')
])
# Printing unzipped lists
print(l1)
print(l2)