Open In App

Python Convert Set To List Without Changing Order

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

Sets in Python are unordered collections that store unique elements. This characteristic makes them valuable for various tasks, but we need to convert a set to a list it’s very important to maintain their order. In this article, we will explore unique approaches to convert a set to a list without changing order in Python.

Convert Set To List Without Changing Order in Python

Below are the possible approaches to convert set to list without changing the order In Python

  • Using the list() constructor
  • Using the sorted() function
  • Using a loop with append()
  • Using collections.OrderedDict

Convert Set To List Using the list() Constructor

The below approach uses the list() constructor, which takes the set as an argument, converting each unique element from the set into a list while preserving the original order of elements in the set. The resulting list reflects the order of elements as in the original set.

Python3




#Set items
gfg_articles_set = {'Python', 'Java', 'C', 'C++', 'JavaScript', 'Ruby'}
 
# Convert the set to a list using the list() constructor
gfg_articles_list = list(gfg_articles_set)
 
# Display the original set and the resulting list
print("Original Set:", gfg_articles_set)
print("Converted List:", gfg_articles_list)


Output

Original Set: {'JavaScript', 'Ruby', 'C++', 'Java', 'Python', 'C'}
Converted List: ['JavaScript', 'Ruby', 'C++', 'Java', 'Python', 'C']

Convert Set To List Using the sorted() Function

The below approach uses the sorted() function, which takes the set as an argument, creating a sorted list of elements from the set in ascending order. The resulting list reflects the sorted order of elements from the original set.

Python3




# Set items
gfg_articles_set = {'Python', 'Java', 'C', 'C++', 'JavaScript', 'Ruby'}
 
# Convert the set to a sorted list using the sorted() function
gfg_articles_list = sorted(gfg_articles_set)
 
# Display the original set and the resulting sorted list
print("Original Set:", gfg_articles_set)
print("Sorted List:", gfg_articles_list)


Output

Original Set: {'C', 'Python', 'JavaScript', 'C++', 'Ruby', 'Java'}
Sorted List: ['C', 'C++', 'Java', 'JavaScript', 'Python', 'Ruby']

Convert Set To List Using a Loop with append()

The below approach initializes an empty list and uses a loop with the append() method to iterate over the elements of the set, adding each element to the list. The resulting list maintains the order of elements from the original set.

Python3




# Define a set with programming languages related to GeeksforGeeks
gfg_articles_set = {'Python', 'Java', 'C', 'C++', 'JavaScript', 'Ruby'}
 
# Initialize an empty list
gfg_articles_list = []
 
# Convert the set to a list using a loop with append()
for articles in gfg_articles_set:
    gfg_articles_list.append(articles)
 
# Display the original set and the resulting list
print("Original Set:", gfg_articles_set)
print("Converted List:", gfg_articles_list)


Output

Original Set: {'Python', 'C', 'C++', 'Java', 'JavaScript', 'Ruby'}
Converted List: ['Python', 'C', 'C++', 'Java', 'JavaScript', 'Ruby']

Convert Set To List Using collections.OrderedDict:

The below approach uses collections.OrderedDict.fromkeys() to create an ordered dictionary from the set, and then converting it to a list using the list() constructor. This method removes any duplicate elements and preserves the order of the original set when converting it to a list.

Python3




from collections import OrderedDict
 
# Define a set with programming languages related to GeeksforGeeks
gfg_articles_set = {'Python', 'Java', 'C', 'C++', 'JavaScript', 'Ruby'}
 
# Convert the set to a list using collections.OrderedDict
gfg_articles_list = list(OrderedDict.fromkeys(gfg_articles_set))
 
# Display the original set and the resulting list
print("Original Set:", gfg_articles_set)
print("Converted List:", gfg_articles_list)


Output

Original Set: {'JavaScript', 'Ruby', 'C', 'Java', 'Python', 'C++'}
Converted List: ['JavaScript', 'Ruby', 'C', 'Java', 'Python', 'C++']

Conclusion

In conclusion, the best approach depends on your specific needs and the initial data structure. If order doesn’t matter, use list(my_set). If you have a predefined order, use a loop or sorted(). For maintaining insertion order, use collections.OrderedDict. Choose the method that suits your requirements for efficient and clear code.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads