Generating a Set of Tuples from a List of Tuples in Python
Last Updated :
20 Feb, 2024
Python provides a versatile set of tools for manipulating data structures, and when it comes to working with tuples, developers often need to extract unique elements or create a set of tuples from a given list. In this article, we will explore some simple and commonly used methods to generate a set of tuples from a list of tuples in Python.
Generating a “Set Of Tuples” from A “List of Tuples”
Below, are the methods of Generating A “Set Of Tuples” From A “List Of Tuples” in Python.
Generating A “Set Of Tuples” From A “List Of Tuples” Using the set() Function
In this example, the below code creates a list of tuples named `list_of_tuples`. It then converts the list into a set, removing any duplicate tuples, and assigns it to `set_of_tuples`. Finally, it prints the resulting set, showcasing the unique tuples from the original list.
Python3
list_of_tuples = [( 1 , 2 ), ( 3 , 4 ), ( 1 , 2 ), ( 5 , 6 )]
set_of_tuples = set (list_of_tuples)
print (set_of_tuples)
|
Output
{(1, 2), (3, 4), (5, 6)}
Generating A “Set Of Tuples” From A “List Of Tuples” Using a Dictionary & List Comprehension
In this example, below code removes duplicate tuples from the `list_of_tuples` by creating a dictionary (`unique_dict`) using tuple elements as keys. Then, it generates a set of tuples (`set_of_tuples`) by converting the keys of the dictionary back to a list. Finally, it prints the resulting set.
Python3
list_of_tuples = [( 1 , 2 ), ( 3 , 4 ), ( 1 , 2 ), ( 5 , 6 )]
unique_dict = dict .fromkeys(list_of_tuples)
set_of_tuples = set (unique_dict.keys())
print (set_of_tuples)
|
Output
{(1, 2), (3, 4), (5, 6)}
Generating A “Set Of Tuples” From A “List Of Tuples” Using the itertools library
In this example, below code eliminates duplicate tuples from the `list_of_tuples` by first sorting it. Then, it utilizes the `groupby` function from the `itertools` module to group similar tuples together. Finally, it creates a set of tuples (`set_of_tuples`) by selecting the first tuple from each group, and prints the resulting set.
Python3
from itertools import groupby
list_of_tuples = [( 1 , 2 ), ( 3 , 4 ), ( 1 , 2 ), ( 5 , 6 )]
sorted_list = sorted (list_of_tuples)
set_of_tuples = { next (group) for _, group in groupby(sorted_list)}
print (set_of_tuples)
|
Output
{(1, 2), (3, 4), (5, 6)}
Generating A “Set Of Tuples” From A “List Of Tuples” Using a Custom Function
In this example, below code defines a custom function `unique_tuples` that takes a list of tuples and generates a set of unique tuples while preserving their original order. It uses a set (`seen`) to keep track of encountered tuples and appends each unique tuple to the result list.
Python3
list_of_tuples = [( 1 , 2 ), ( 3 , 4 ), ( 1 , 2 ), ( 5 , 6 )]
def unique_tuples(input_list):
seen = set ()
result = set ()
for item in input_list:
if item not in seen:
seen.add(item)
result.add(item)
return result
set_of_tuples = unique_tuples(list_of_tuples)
print (set_of_tuples)
|
Output
{(1, 2), (3, 4), (5, 6)}
Conclusion
In conclusion, Generating a set of tuples from a list of tuples in Python can be achieved through various methods, each with its own advantages. Whether you prefer the simplicity of the set()
function, the control of a custom function, or the features of libraries like itertools
, choosing the right method depends on your specific requirements and preferences. Experiment with these approaches to find the one that best fits your needs.
Share your thoughts in the comments
Please Login to comment...