The different ways to use iterators in python for data science are as follows:
C-style: For using this, we need to know the total number of iterations.
Points to remember:
- It is a rarely use style.
- It is a 4 step approach, and no compactness is create with single view looping construct.
- In the case of large scale programs and designs, it is prone to errors.
- For loops in Python, there is no C-Style.
Using for-in or for each style:
This is use when we are dealing with iterators of lists, dictionary, n- dimensional arrays, etc. So, each component is fetch by the iterators and data printing is done while looping. There is an automatic increment or decrement in the iterators in this construct.
Indexing by use of range function:
Indexing can be use by range() function.
It is a built-in function in Python taking input as iterators, list, etc. and returns a tuple that contains index and data at the index in the sequence of the iterator.
So, we can also write it in the following way:
The return value of enumerate() can be print directly and the return can be observe.
Parameter start is taken by enumerate which by default is set to zero. This parameter can be alter to any value. Let us look at an example:
- Use of two iterators for a single looping construct: Here a list and a dictionary are use for each and every iteration in a single looping block with the help of enumerate function.
- zip function: It comes in handy when we need to combine similar types of iterators(list-list or dict-dict) data items at the with position. So, the shortest length of input iterators is use. Items of larger length iterators are skip. When the iterator is empty no output is generate. We should keep in mind that both the iterators should be use in single looping construct.
The reverse of zip function is unzipping, it can be perform using “*” operator.