How to Convert a List into Ordered Set in Python? Last Updated : 07 Jan, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report The task is to turn a list into an ordered set, meaning we want to remove any duplicate items but keep the order in which they first appear. In this article, we will look at different ways to do this in Python.Using OrderedDict.fromkeys()OrderedDict.fromkeys() method removes duplicates from a list and keeps the original order of the elements. It works by using an OrderedDict, which remembers the order of items as they are added.Example: Python from collections import OrderedDict # list a = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3] # Remove duplicates while preserving order res = list(OrderedDict.fromkeys(a)) # Print the result print(res) Output[3, 1, 4, 5, 9, 2, 6] Using dict.fromkeys()dict.fromkeys() method removes duplicates from a list while preserving the order of elements. Since dictionaries in Python 3.7+ maintain insertion order.Example: Python # list a = [1, 2, 3, 2, 1, 4, 5] # Remove duplicates while preserving order res = list(dict.fromkeys(a)) print(res) Output[1, 2, 3, 4, 5] Using for loop for loop iterate through the list, tracking seen elements with a set. It removes duplicates while preserving the order of the original list.Example: Python # list a = [1, 2, 3, 2, 1, 4, 5] # Initialize an empty list to store the result res = [] # Initialize set `s` to track seen elements s = set() # Iterate through `a` for i in a : # If the item is not already in `s` if i not in s: # Add the item to the `res` res.append(i) # Mark item as seen by adding it to `s` s.add(i) print(res) Output[1, 2, 3, 4, 5] Comment More infoAdvertise with us Next Article Convert set into a list in Python V venkateshk1220 Follow Improve Article Tags : Python Python Programs python-list Python set-programs Practice Tags : pythonpython-list Similar Reads Convert set into a list in Python In Python, sets are unordered collections of unique elements. While they're great for membership tests and eliminating duplicates, sometimes you may need to convert a set into a list to perform operations like indexing, slicing, or sorting. For example, if input set is {1, 2, 3, 4} then Output shoul 3 min read Convert set into a list in Python In Python, sets are unordered collections of unique elements. While they're great for membership tests and eliminating duplicates, sometimes you may need to convert a set into a list to perform operations like indexing, slicing, or sorting. For example, if input set is {1, 2, 3, 4} then Output shoul 3 min read Convert Set of Tuples to a List of Lists in Python Sets and lists are two basic data structures in programming that have distinct uses. It is sometimes necessary to transform a collection of tuples into a list of lists. Each tuple is converted into a list throughout this procedure, and these lists are subsequently compiled into a single, bigger list 3 min read Python - Sort list according to other list order Sorting a list based on the order defined by another list is a common requirement in Python, especially in scenarios involving custom sorting logic. This ensures that elements in the first list appear in the exact order specified in the second list. Python provides various methods to accomplish this 2 min read Python Convert Dict of Lists to Dict of Sets In Python, converting a dictionary of lists to a dictionary of sets is a valuable process for optimizing data structures. This transformation enhances efficiency by eliminating duplicates and enabling efficient membership testing. Utilizing built-in functions like set() and list comprehension simpli 3 min read Python - Convert List of lists to list of Sets We are given a list containing lists, and our task is to convert each sublist into a set. This helps remove duplicate elements within each sublist. For example, if we have:a = [[1, 2, 1], [1, 2, 3], [2, 2, 2, 2], [0]]The output should be:[{1, 2}, {1, 2, 3}, {2}, {0}]Let's explore some method's to ac 2 min read Like