Sunday, November 17, 2024
Google search engine
HomeLanguagesPython Ordered Set

Python Ordered Set

We are going to learn about how can we create the order set by using different methods using Python. An ordered set is a data structure in which the data order can be preserved and it is used when we require the position of data to remain fixed in the order we have inserted. It is to be noted that, We are able to iterate the ordered set which means the ordered set once we declare the position of the value is fixed. But in the set, you will get a subscribable error while iterating because positions are not fixed in the set.

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 randomposition 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.

There are three methods to create the ordered sets in Python:

  • By using a dictionary data structure 
  • By using the list data structure
  • By using the ordered set module(or class) 

Ordered set 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:

Python3




# Creation of ordered set using the dict data structure
dictionary = {"Prince": "", "Aditya": "",
              "Praveer": "", "Prince": "", "Shiv": ""}
print(dictionary)
# For accessing only keys from the dictionary
for key in dictionary.keys():
    print(key, end=" ")


Output:

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

Ordered set using the list data structure

We can use the list data structure to create the order set by using removing the duplicate elements from it. Let’s take a look at implementation as explained below: 

Python3




def removeduplicate(data):
    countdict = {}
    for element in data:
        if element in countdict.keys():
             
            # increasing the count if the key(or element)
            # is already in the dictionary
            countdict[element] += 1
        else:
            # inserting the element as key  with count = 1
            countdict[element] = 1
    data.clear()
    for key in countdict.keys():
        data.append(key)
 
 
dataItem = ["Prince", "Aditya", "Praveer", "Prince", "Aditya", "Shiv"]
print("Before removing duplicate elements from dataItems", dataItem)
removeduplicate(dataItem)
print("Created ordered set by removing duplicate elements", dataItem)


Output:

Before removing duplicate elements from dataItems ['Prince', 'Aditya', 'Praveer', 'Prince', 'Aditya', 'Shiv']
Created ordered set by removing duplicate elements ['Prince', 'Aditya', 'Praveer', 'Shiv']

Ordered set 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 orderedSet:

orderedSet(Listname)

Example:

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

Python3




from ordered_set import OrderedSet
 
createOrderedSet = OrderedSet(
    ['GFG', 'is', 'an', 'Excellent',
     'Excellent', 'platform'])
 
print(createOrderedSet)
 
# we are able to iterate it similar to list data type
for index in range(len(createOrderedSet)):
    print(createOrderedSet[index], end=" ")


Output:

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

Time complexity: O(n), where n is the number of elements in the Ordered Set

Auxiliary space: O(n), where n is the number of elements in the Ordered  Set 

RELATED ARTICLES

Most Popular

Recent Comments