Python Itertools Tutorial – Part V

Articles From: QuantInsti
Website: 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:

The output would be as follows:


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:

The output will be as follows:

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

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

Visit for ready-to-use Python functions as applied in trading and data analysis.

Disclosure: Interactive Brokers

Information posted on IBKR Campus that is provided by third-parties does NOT constitute a recommendation that you should contract for the services of that third party. Third-party participants who contribute to IBKR Campus 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 its permission. The views expressed in this material are solely those of the author and/or QuantInsti and Interactive Brokers 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 buy or sell any security. It should not be construed as research or investment advice or a recommendation to buy, sell or hold any security or commodity. 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.