May 12, 2018

7 Ways to Create Sequences in Python

For, Iters, Recursion, Maps, Etc.

Background

A sequence in mathematics, is a collection (like a set) of mathematical objects where the order of the objects is significant, and duplicate members of the collection are allowed. In computer science, we represent sequences as arrays, lists, streams, and a variety of other data structures. Creating sequences and operations on already existing sequences is at the core of many software applications. In this post, we’ll look at some techniques for creating sequences using Python 3. The methods will vary based on the sequence we want to produce. Some sequences are infinite, some depend on their index, some depend on previous values in the sequence.

Note: The sequences in this post are any generic type of iterable thing that matches the mathematical defintion of a sequence above. I am not talking about the specific Python notion of a sequence. For the Python version of a sequence, see here.

For-Loop

Sometimes when we know the length of a finite sequence, it is easy to write a simple for-loop to populate the sequence. Consider the sequence consisting of the first n values of the Fibonacci Sequence. The nth value in the Fibonacci sequence is given by the sum of the two previous values in the sequence, i.e. F[n] = F[n - 1] + F[n - 2].

Recursion

A function is recursive if it calls itself with new arguments, eventually leading to some terminating condition resulting in a value being returned from the intial function call. For example, our fibonacci sequence can also be defined recursively. Notice how the inner function recurse calls itself:

Create a Map Function

In functional programming, a map function takes a n input structure parameterized by type T1, and a mapping function from type T1 to type T2. Calling this map function will call the passed in mapping function on each value of the input structure and return an output structure parameterized by type T2. That sounds complicated, but in practice it’s pretty easy! For example, if we have a list of Rectangles with width and heights, and we call map passing in an area function that returns width * height, we end up with a list of areas.

Generator

A generator function is a special function in Python, that can yield multiple values, instead of just a single return. Calling the generator creates an iterator, which can be iterated through with a for-loop.

Generators are computed lazily. That means the next value isn’t calculated until you ask for it. This also means generators can represent infinite sequences. For example, the following generator represents all square numbers:

Recent Posts

About Camden

Camden is currently a software developer at Liazon. When he isn't spending time with his family, he enjoys programming the full web stack, and experimenting with new frameworks/languages. Recently he has been especially interested in functional programming and data science. Camden can be contacted via e-mail at camdenreslink@gmail.com, or through his LinkedIn Profile.