Following are the different ways to use iterators.
C-style approach This approach requires prior knowledge of the total number of iterations.
|
Output:
Aston Audi McLaren
Important points:
- This style of looping is rarely used by python programmers.
- This four-step approach does not lend itself to compactness with a single-view looping construct.
- This is also error prone in large scale programs or projects.
- Python has no C-style for looping , i.e. a loop similar to for (int i = 0; i "n; i ++)
Using the for-in style (or for each ):
This style is used in python containing list iterator, dicton, n-dimensional arrays, etc. The iterator fetches each component and prints the data during cycle. The iterator is automatically incremented / decremented in this construct.
|
Exit:
Aston Audi McLaren
See this for more examples of different data types.
Indexing Using Range: We can also index using range () in Python.
| tr>
Exit :
Aston Audi McLaren
Enumerate:
Enumerate — it is a built-in Python function that takes input as an iterator, list, etc., and returns a tuple containing the index and that index’s data in the iterator sequence. For example, enumerate (cars), returns an iterator that will return (0, cars [0]), (1, cars [1]), (2, cars [2]), and so on.
|
Output:
Aston Audi McLaren
Below solution also works.
|
Exit :
(0, ’Aston’) (1,’ Audi’) (2, ’McLaren’)
We can also directly print the return value of enumerate () to see what it returns.
|
Output:
[(0, ’Aston’), (1,’ Audi’), (2, ’McLaren’)]
Enumerate takes a start parameter, which is set to zero by default. We can change this parameter to whatever value we like. In the code below, we have used the beginning as 1.
| tr>
Output:
(1, ’Aston’) (2, ’Audi’) (3,’ McLaren ’)
enumerate () helps in building a solution for accessing each item of data in an iterator and retrieving the index of each item.
Loop extensions:
i) Two iterators for one looping construct: in this case the list and dictionary must be used for each iteration in one looping block using the enumeration function. Let’s see an example.
|
Exit:
Car: Aston Price: 570,000 $ Car: Audi Price: 68,000 $ Car: McLaren Price: 450,000 $ Accessory: GPS kit Price: 8900 $ Accessory: Car repair tool kit Price: 4500 $
ii) zip function (both iterators must be used in a single looping construct):
This function is useful for combining the same iterator types (list-list or dict dict, etc.) in data items in i-th position. It uses the shortest length of these input iterators. Other elements of longer iterators are skipped. In the case of empty iterators, it returns No output.
For example, using zip for two lists (iterators) helped to combine one machine and its required accessory.
|
Exit:
Car: Aston, Accessory required: GPS Car: Audi, Accessory required : Car Repair Kit Car: McLaren, Accessory required: Dolby sound kit
The downside of these iterators from the zip function is known as unzipping with using the "*" operator.
Using the enumeration function and the zip function helps to achieve an efficient extension of iterative logic in python and solves many more sub-tasks of a huge task or problem.
|
Exit:
(’Aston’,’ Audi’, ’McLaren’) (’ GPS ’,’ Car Repair’, ’Dolby sound kit’)
Links:
1. https://docs.python.org/2/library/functions.html#enumerate
2. < a href = https://docs.python.org/2/library/functions.html#zip> https://docs.python.org/2/library/functions.html#zip
This article contributed by Krishnasagar Podadraj . If you like Python.Engineering and would like to contribute, you can also write an article and mail it to [email protected] See my article appearing on the Python.Engineering homepage and help other geeks.
Please post comments if you find anything wrong or if you would like to share more information on the topic discussed above.