Open In App

How to Convert a List into Ordered Set in Python?

Last Updated : 29 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

One such data structure that often proves useful is the ordered set. An ordered set combines the characteristics of both a set and a list, ensuring the uniqueness of elements while preserving the order in which they were added. In this article, we will explore the concept of an ordered set and some different methods for converting a list into this specialized data structure.

What is an Ordered Set?

An ordered set is a collection of unique elements where the order of insertion is maintained. This means that while elements are distinct, the sequence in which they were added remains intact. In Python, the collections the module provides a dedicated class for ordered sets called OrderedDict.

Syntax:

from collections import OrderedDict

ordered_set = OrderedDict()

Converting a List into an Ordered Set in Python

Below, are the methods of Converting A List Into An Ordered Set in Python:

  • Using For Loop
  • Using sorted() Method
  • Using list(dict.fromkeys()) Method
  • Using OrderedDict.fromkeys() Method

Converting A List Into An Ordered Set Using For Loop

In this example, The code uses an ordered set implementation (`OrderedDict`) to convert a list into an ordered set, preserving element uniqueness and insertion order. The example demonstrates this by creating an ordered set from the input list and printing the types of both the original list and the resulting ordered set.

Python3




from collections import OrderedDict
 
 
def list_to_ordered_set(input_list):
    ordered_set = OrderedDict()
    for item in input_list:
        ordered_set[item] = None
    return set(ordered_set.keys())
 
 
# Example usage
input_list = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3]
result = list_to_ordered_set(input_list)
print(type(input_list))
print("Output:", result)
print(type(result))


Output

<class 'list'>
Output: {1, 2, 3, 4, 5, 6, 9}
<class 'set'>


Converting A List Into An Ordered Set Using sorted() Method

In this example, below code utilizes the `set()` and `sorted()` combination to convert a list into an ordered set, maintaining the original order of elements. The example demonstrates this process by creating an ordered set from the input list and printing the types of both the original list and the resulting ordered set.

Python3




def list_to_ordered_set(input_list):
    return set(sorted(set(input_list), key=input_list.index))
 
# Example usage
input_list = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3]
result = list_to_ordered_set(input_list)
print(type(input_list))
print("Output:", result)
print(type(result))


Output

<class 'list'>
Output: {1, 2, 3, 4, 5, 6, 9}
<class 'set'>


Converting A List Into An Ordered Set Using list(dict.fromkeys()) Method

In this example, below code converts a list into an ordered set using the `dict.fromkeys()` idiom, ensuring uniqueness while preserving the original order of elements. The example demonstrates this by creating an ordered set from the input list and printing the types of both the original list and the resulting ordered set.

Python3




def list_to_ordered_set(input_list):
    return set(dict.fromkeys(input_list))
 
# Example usage
input_list = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3]
result = list_to_ordered_set(input_list)
print(type(input_list))
print("Output:", result)
print(type(result))


Output

<class 'list'>
Output: {1, 2, 3, 4, 5, 6, 9}
<class 'set'>


Converting A List Into An Ordered Set Using OrderedDict.fromkeys() Method

In this example, below code employs the `OrderedDict.fromkeys()` method to convert a list into an ordered set, preserving the original order of elements. The example demonstrates this by creating an ordered set from the input list and printing the types of both the original list and the resulting ordered set.

Python3




from collections import OrderedDict
 
def list_to_ordered_set(input_list):
    return set(OrderedDict.fromkeys(input_list))
 
# Example usage
input_list = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3]
result = list_to_ordered_set(input_list)
print(type(input_list))
print("Output:", result)
print(type(result))


Output

<class 'list'>
Output: {1, 2, 3, 4, 5, 6, 9}
<class 'set'>


Conclusion

Converting a list into an ordered set is a common task in programming, especially when dealing with unique elements and maintaining their order of insertion. In Python, the OrderedDict class from the collections module provides a convenient solution. By exploring the four methods outlined in this article, programmers can choose the approach that best fits their specific use case, balancing considerations of efficiency and code readability.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads