# Python Itertools Tutorial – Part V

Contributor:
QuantInsti
Visit: QuantInsti

Get up-to-speed in this series with Part IPart IIPart III and Part IV.

The islice() iterator

This iterator has four parameters which can be passed, the element, starting element variable, ending variable and the number of elements to be skipped.

Suppose, we wanted the Adj. Close price every third day, we would write the code as follows:

# Islice() itertool
# data = tesla[‘daily_returns’]
selection = itertools.islice(tesla[‘Adj Close’], 0, 15,2)
for each in selection:
print(each)

The output would be as follows:

743.6199951171875
749.5
703.47998046875
645.3300170898438
560.5499877929688
445.07000732421875
361.2200012207031
427.5299987792969

Well, these were some terminating iterators. Now, we will see the next type of iterators, which are more concerned with the selection and the arrangement of the values.

Combinatoric iterators

We have all studied permutations and combinations before. These iterators make it really easy to list down all the possible values in a simple manner. Let’s start with the first one right away.

The combinations() iterator

As the name specifies, this iterator helps us illustrate all the possible combinations present in the list. The only parameters we have to pass are the elements and the number of values in a combination. Let’s see it in action right now:

# Combinations itertool
stocks_NYSE = [‘TSLA’, ‘MSFT’, ‘NVDA’, ‘GOOGL’ , ‘AAPL’ , ‘INTC’]
result = itertools.combinations(stocks_NYSE, 2)
for each in result:
print(each)

The output will be as follows:

(‘TSLA’, ‘MSFT’)
(‘TSLA’, ‘NVDA’)
(‘TSLA’, ‘GOOGL’)
(‘TSLA’, ‘AAPL’)
(‘TSLA’, ‘INTC’)
(‘MSFT’, ‘NVDA’)
(‘MSFT’, ‘GOOGL’)
(‘MSFT’, ‘AAPL’)
(‘MSFT’, ‘INTC’)
(‘NVDA’, ‘GOOGL’)
(‘NVDA’, ‘AAPL’)
(‘NVDA’, ‘INTC’)
(‘GOOGL’, ‘AAPL’)
(‘GOOGL’, ‘INTC’)
(‘AAPL’, ‘INTC’)

In the next installment, the author will discuss the combinations_with_replacement() iterator.

Visit https://www.quantinsti.com/ for ready-to-use Python functions as applied in trading and data analysis.

##### Disclosure: Interactive Brokers

Information posted on IBKR Traders’ Insight that is provided by third-parties and not by Interactive Brokers does NOT constitute a recommendation by Interactive Brokers that you should contract for the services of that third party. Third-party participants who contribute to IBKR Traders’ Insight are independent of Interactive Brokers and Interactive Brokers does not make any representations or warranties concerning the services offered, their past or future performance, or the accuracy of the information provided by the third party. Past performance is no guarantee of future results.

This material is from QuantInsti and is being posted with permission from QuantInsti. The views expressed in this material are solely those of the author and/or QuantInsti and IBKR is not endorsing or recommending any investment or trading discussed in the material. This material is not and should not be construed as an offer to sell or the solicitation of an offer to buy any security. To the extent that this material discusses general market activity, industry or sector trends or other broad based economic or political conditions, it should not be construed as research or investment advice. To the extent that it includes references to specific securities, commodities, currencies, or other instruments, those references do not constitute a recommendation to buy, sell or hold such security. This material does not and is not intended to take into account the particular financial conditions, investment objectives or requirements of individual customers. Before acting on this material, you should consider whether it is suitable for your particular circumstances and, as necessary, seek professional advice.

In accordance with EU regulation: The statements in this document shall not be considered as an objective or independent explanation of the matters. Please note that this document (a) has not been prepared in accordance with legal requirements designed to promote the independence of investment research, and (b) is not subject to any prohibition on dealing ahead of the dissemination or publication of investment research.

Any trading symbols displayed are for illustrative purposes only and are not intended to portray recommendations.