A finite iterator will terminate based on the length of the shortest input sequence. Finite iterators are great for sequences that we need to have terminated to prevent infinite loops or memory leakage.

A useful itertool that is a finite iterator is the chain() itertool. This finite iterator will take in one to multiple iterables and combine them into a single iterator. Here is what the base syntax looks like:


The input value of chain() is one or more iterables of the same or varying iterable types. For example, we could use the chain() itertool to combine a list and a set into one iterable.

The return type of chain() is an iterator, so if we want to use the returned object directly, it must be explicitly converted into an iterable type (list, set, dictionary, etc) first.

To show how it’s used in a scenario, suppose we want to combine a list containing odd numbers and a set containing even numbers:

import itertools odd = [5, 7, 9] even = {6, 8, 10} all_numbers = list(itertools.chain(odd, even)) print(all_numbers)
  • Import our itertools module.
  • Supply the iterable list and set we want to combine to chain(). Convert it from an iterator to a list so that we can access the result directly.
  • Print the result which will be:
    [5, 7, 9, 8, 10, 6]

Let’s use chain() to work with SKU iterables in our pet store!



We have separate lists of SKUs for each bag of dog food per brand. Obtain a master list of SKU numbers for all bags of dog food, regardless of brand.

Use the chain() itertool set to a variable named all_skus_iterator to combine the SKU lists.


Since the return value of calling chain() is an iterator, let’s convert the returned all_skus_iterator iterator object into a list object called all_skus_list.


Lastly, print the resulting all_skus_list to see the combined, final list.

Sign up to start coding

By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.
Already have an account?