Select Page

Programming is mainly about handling data. As a Python developer, you’ll find ways to store data in a manner that is consistent with your objectives. Sometimes, you’ll need to preserve the order of data insertion in a set, for example, if you are handling bank transactions. Each transaction has to be unique, and it is important to preserve the order in which transactions are created. Python’s ordered sets help you to do just that.

In this article, we will explain the programming concept of an ordered set, before showing you how to create one in a Python program.

What Is a Set in Python?

In the Python programming language, a set is a collection of unique elements. It is a hash table-based data structure with undefined element ordering. You can browse a set’s elements, add or remove them, and perform the standard set operations of union, intersection, complement, and difference.

Unlike lists, ordinary sets do not preserve the order in which we insert the elements. This is because the elements in a set are usually not stored in the order in which they appear. 

What Is an Ordered Set?

Unlike in a standard set, the order of the data in an ordered set is preserved. We used ordered sets when we needed the order in which we entered the data to be maintained over the course of the program. In an ordered set, looking at the data does not change its order as it would in an unordered set.

How To Create an Ordered Set in Python

Python allows you to create ordered sets in your programs. Below we’ll demonstrate two ways to do so: using Python’s ordered-set package, and a manual method. But first, let’s establish a context. Let’s say you’re developing an app for a bank in which you need to record transaction numbers one after another in a summary document. Each bank transaction operation is unique.

Also, you want the order in which transactions are made to reflect exactly in your data set. This is a perfect opportunity for you to use the OrderedSet class included in Python’s ordered_set package.

Python’s Ordered Set Class

The simplest way to create an ordered set in Python is to use the OrderedSet class. Note that this class is not included by default. You first need to make sure you have the ordered-set package installed.

Note that ordered-set is a third-party package, and its functionality can change independently of the version of Python that you’re using. To install the package, type the following command in your terminal:

pip install ordered-set


This will enable you to use the OrderedSet class.

Now, you can create a Python program that uses the OrderedSet class. Let’s see what a simple ordered set looks like: 

from ordered_set import OrderedSet
setTest = OrderedSet(["First", "Second", "Second", "Third"])
print(setTest)

First, we import the freshly installed ordered_set package. Then, we create an object off the OrderedSet class, passing the members as parameters. The print statement in this example outputs the following: 

OrderedSet(['First', 'Second', 'Third'])

The string ‘Second’ that we entered twice when creating the set is now gone, while the order in which we entered data is maintained.

Now, let’s create an ordered set of bank transactions. In a real-world scenario, you would want to keep the order of insertion in place, to allow you to analyze the transactions, check for fraud, and so forth. Here is how the program might look:

from ordered_set import OrderedSet
bankStatement = OrderedSet(["BK0001","BK0002","BK0003","BK0004","BK0005"])...

The ordered set is created.

Now, if you want to a given transaction, you could select the corresponding set of items using its index:

...print("Transaction no",bankStatement[1],"has been recorded successfully")

This gives you the following output:

Transaction no BK0002 has been recorded successfully

But what if someone wanted to add a transaction that has already been recorded, such as “BK0004”? If we had used a list, this action would have been possible. Fortunately, the ordered set does not allow it. Let’s run the following code: 

bankTrs.add("BK0004")

print(bankTrs)

The result of the print statement remains unchanged, proving that the ordered set disregarded the action:

OrderedSet(['BK0001', 'BK0002', 'BK0003', 'BK0004', 'BK0005'])


This feature proves particularly useful in this case. As a programmer, you won’t have to worry about ensuring that each data member is unique.

The ordered-set package contains other noteworthy features. It allows you to perform useful operations like difference, intersection and union using the operators -, & and |

Set Operations

Let’s rewrite the program to create two different ordered sets that could represent two bank statements.

from ordered_set import OrderedSet
bankStatement1 = OrderedSet(["BK0001","BK0002","BK0003","BK0004","BK0005"])
bankStatement2 = OrderedSet(["BK0004","BK0005","BK0006","BK0007","BK0008"])

We deliberately included the transactions BK0004 and BK0005 in both statements. That could be the case if the first and the second statement partially cover the same time period.

If you want to see the transactions that exist only in the bankStatement1, just run the following bit of code:

diff  = bankStatement1 - bankStatement2
print("The transactions unique to the first summary are",diff)

This gives us the following result:

The transactions unique to the first summary are OrderedSet(['BK0001', 'BK0002', 'BK0003'])

For readability purposes, we can enclose the ordered set inter within a list when displaying the data using this code:

diff  = bankStatement1 - bankStatement2
print("The transactions unique to the first summary are",list(diff))

Now, if you need to retrieve only the transactions that exist in both statements, use the intersection statement like so:

inter = bankStatement1 & bankStatement2
print("The transactions common to both summaries are",list(inter))

You’ll get the intended result:

The transactions common to both summaries are OrderedSet['BK0004', 'BK0005']

Finally, if you wish to see all the transactions of both statements, simply perform the union operation:

union = bankStatement1 | bankStatement2
print("Here are all the transactions of these summaries:",lis(union))

This will give you the following output:

Here are all the transactions of these summaries: OrderedSet['BK0001', 'BK0002', 'BK0003', 'BK0004', 'BK0005', 'BK0006', 'BK0007', 'BK0008']

The ordered_set package makes creating and manipulating ordered sets in Python simple and effective.

The Manual Method

It is also possible to create an ordered set of data entirely manually. In case you are not able to use the ordered-set package, you can still use this workaround. Let’s see how this method works. 


First, we’ll create a string array containing our set of data:

bankStatement=["BK0001","BK0002","BK0003","BK0004","BK0004","BK0005","BK0006"]


Then, we create a for loop that checks each element, looking for duplicates. If there are any, they will be removed from the set. To test this, we’ll deliberately include a duplicate element in the array.

for string in range(len(bankStatement), 1, -1):

    if bankStatement[string-1] in bankStatement[:string-1]:
        bankStatement.pop(string-1)

The for loop starts iterating from the back of the list, that is, from the last element. It takes that element (called string above) and checks whether it is already present in the subset of the list up until but not including the current element (string). If it is already present, we remove the mention of the element closer to the front of the list, but keep the original mention of the element closer to the back of the list.

Now, when we print the array content, there are no duplicates and the order is maintained:

['BK0001', 'BK0002', 'BK0003', 'BK0004', 'BK0005', 'BK0006']

This allows us to create an ordered set even if we cannot use Python’s dedicated feature!

Learn To Code Online

Python is a versatile programming language with a few options for creating ordered sets. You can use the OrderedSet class to get the job done, or you can do so manually if needed.

Want to go beyond ordered set creation in Python?

Udacity’s expert-designed Introduction to Programming Nanodegree program is your next step. By the end of this course, you’ll know the basics of coding and have the skills to confidently manage real-world programming scenarios using HTML, CSS, Python, and more!

Complete Code Examples

Example 1: Bank Transaction Ordered Set Creation

from ordered_set import OrderedSet
bankStatement = OrderedSet(["BK0001","BK0002","BK0003","BK0004","BK0005"])

print("Transaction no",bankStatement[1],"has been recorded successfully")
bankTrs.add("BK0004")

print(bankTrs)

Example 2: Differente, Union, Intersection

from ordered_set import OrderedSet
bankStatement1 = OrderedSet(["BK0001","BK0002","BK0003","BK0004","BK0005"])
bankStatement2 = OrderedSet(["BK0004","BK0005","BK0006","BK0007","BK0008"])

diff  = bankStatement1 - bankStatement2
print("The transactions unique to the first summary are",list(diff))

inter = bankStatement1 & bankStatement2

print("The transactions common to both summary are",list(inter))

union = bankStatement1 | bankStatement2

print("Here are all the transactions of these summaries:",list(union))

Example 3: The Manual Method

bankStatement=["BK0001","BK0002","BK0003","BK0004","BK0004","BK0005","BK0006"]

for string in range(len(bankStatement), 1, -1):

    if bankStatement[string-1] in bankStatement[:string-1]:
        bankStatement.pop(string-1)

print(bankStatement)