Open In App

Ordered Set - Python

Last Updated : 22 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

An ordered set is a collection that combines the properties of both a set and a list. Like a set, it only keeps unique elements, meaning no duplicates are allowed. Like a list, it keeps the elements in the order they were added.

In Python, the built-in set does not maintain the order of elements, while a list allows duplicates. An ordered set solves this problem by keeping the uniqueness of a set while preserving the order of insertion.

Example:

input_dataSet = {"Prince", "Aditya", "Praveer", "Shiv"}
Output in case of unordered set: {"Aditya, "Prince", "Shiv", "Praveer"}, It can be random position on your side
Output in case of ordered set: {"Prince", "Aditya", "Praveer", "Shiv"}
Explanation: As you know in Python if you print this set more one time than, every time you will getting the random position of the items for the same dataset. But in case of ordered set you will getting the same dataset every time in same order you had inserted items.

Using the Ordered Set Module (or class)

In default, you have an unordered set in Python but for creating the ordered set you will have to install the module named ordered-set by pip package installer as mentioned below:

How to Install the ordered set module

By using the pip package installer download the ordered-set module as mentioned below

pip install ordered_set

Syntax of ordered Set:

orderedSet(Listname)

Now,  for more clarification let's iterate the ordered set because the set cannot be iterated as mentioned below:

Python
from ordered_set import OrderedSet

createOrderedSet = OrderedSet(
    ['GFG', 'is', 'an', 'Excellent', 'Excellent', 'platform'])

print(createOrderedSet)

for item in createOrderedSet:
    print(item, end=" ")

Output:

OrderedSet(['GFG', 'is', 'an', 'Excellent', 'platform'])
GFG is an Excellent platform

Using the Dictionary Data Structure

We can use the dictionary data structure to create the ordered set because the dictionary is itself the ordered data structure in which we will use set items as keys because keys are unique in the dictionary and at the place of value we can create the empty string. Let's take a look at implementation as explained below:

Python
d = {"Prince": "", "Aditya": "",
              "Praveer": "", "Prince": "", "Shiv": ""}
print(d)
for key in d.keys():
    print(key, end=" ")

Output
{'Prince': '', 'Aditya': '', 'Praveer': '', 'Shiv': ''}
Prince Aditya Praveer Shiv 

Using the list Data Structure

A list is a built-in data structure that stores an ordered collection of items. However, unlike a set, lists can contain duplicate values. To create an ordered set using a list, you need to manually remove duplicates while keeping the order of insertion intact. Let's take a look at implementation as explained below: 

Python
def removeduplicate(data):
    countdict = {}
    for element in data:
        if element in countdict.keys():
            countdict[element] += 1
        else:
            countdict[element] = 1
    data.clear()
    for key in countdict.keys():
        data.append(key)


dataItem = ["Prince", "Aditya", "Praveer", "Prince", "Aditya", "Shiv"]

removeduplicate(dataItem)
print(dataItem)

Output
['Prince', 'Aditya', 'Praveer', 'Shiv']

Next Article
Article Tags :
Practice Tags :

Similar Reads