Python | Get unique tuples from list
Last Updated :
01 May, 2023
Sometimes, while working with Python list, we can come across a problem in which we require to find the unique occurrences of list. Having elementary data types is easy to handle, but sometime, we might have complex data types and the problem becomes new in that cases. Let's discuss certain ways in which tuples are handled for this problem.
Method #1 : Using list() + set() Alike integers, being immutable these can also be handled by set() for removal of the duplicates. It converts the list to set and removes duplicates and converted back to list by list()
Python3
# Python3 code to demonstrate working of
# Get unique tuples from list
# using set() + list()
# initializing list
test_list = [(4, 5), (6, 1), (4, 5), (6, 1)]
# printing original list
print("The original list is : " + str(test_list))
# Get unique tuples from list
# using set() + list()
res = list(set(test_list))
# printing result
print("List after removal of duplicates " + str(res))
Time Complexity: O(n)
Space Complexity: O(n)
Output : The original list is : [(4, 5), (6, 1), (4, 5), (6, 1)]
List after removal of duplicates [(4, 5), (6, 1)]
Method #2 : Using dict.fromkeys() + list() Since newer versions of Python dictionaries, remember their order of insertion, the list contents can be converted to dictionary element list, which remembers the order and removes the duplicates. It is converted back using list().
Python3
# Python3 code to demonstrate working of
# Get unique tuples from list
# using dict.fromkeys() + list()
# initializing list
test_list = [(4, 5), (6, 1), (4, 5), (6, 1)]
# printing original list
print("The original list is : " + str(test_list))
# Get unique tuples from list
# using dict.fromkeys() + list()
res = list(dict.fromkeys(test_list))
# printing result
print("List after removal of duplicates " + str(res))
Output : The original list is : [(4, 5), (6, 1), (4, 5), (6, 1)]
List after removal of duplicates [(4, 5), (6, 1)]
Time Complexity: O(n)
Space Complexity: O(n)
Method #3 Use a loop
Step-by-step approach:
- Initialize the input list and a new empty list.
- Loop through each tuple in the input list.
- Check if the tuple is already in the new list.
- If not, append the tuple to the new list.
- Print the original list and the list without duplicates.
Python3
# Python3 code to demonstrate working of
# Get unique tuples from list
# using loop
# initializing list
test_list = [(4, 5), (6, 1), (4, 5), (6, 1)]
# printing original list
print("The original list is : " + str(test_list))
# Get unique tuples from list
# using loop
res = []
for tup in test_list:
if tup not in res:
res.append(tup)
# printing result
print("List after removal of duplicates " + str(res))
OutputThe original list is : [(4, 5), (6, 1), (4, 5), (6, 1)]
List after removal of duplicates [(4, 5), (6, 1)]
Time complexity: O(n^2), because it loops through each tuple in the input list and then loops through the new list to check if the tuple is already present.
Auxiliary space: O(n), because it creates a new list to store the unique tuples.
Similar Reads
Python | Get duplicate tuples from list Sometimes, while working with records, we can have a problem of extracting those records which occur more than once. This kind of application can occur in web development domain. Let's discuss certain ways in which this task can be performed. Method #1 : Using list comprehension + set() + count() In
7 min read
Python | Extract unique tuples from list, Order Irrespective Sometimes, while working with data, we can have a problem in which we need to check for similar records and eradicate them. When elements are ordered, this case has been discussed before. But sometimes, we might have to ignore the order and has to be removed in case similar elements occur. Let's dis
5 min read
Python - Unique Kth positioned tuples Sometimes, while working with Python records, we can have a problem in which we need to extract only the unique tuples, based on some particular index of tuples. This kind of problem can have applications in domains such as web development. Let's discuss certain ways in which this task can be perfor
8 min read
Unzip List of Tuples in Python The task of unzipping a list of tuples in Python involves separating the elements of each tuple into individual lists, based on their positions. For example, given a list of tuples like [('a', 1), ('b', 4)], the goal is to generate two separate lists: ['a', 'b'] for the first elements and [1, 4] for
2 min read
Python - Filter unique valued tuples Given a Tuple list, filter tuples that don't contain duplicates. Input : test_list = [(3, 5, 6, 7), (3, 2, 4, 3), (9, 4, 9), (2, 3, 2)] Output : [(3, 5, 6, 7)] Explanation : Rest all tuples have duplicate values. Input : test_list = [(3, 5, 6, 7, 7), (3, 2, 4, 3), (9, 4, 9), (2, 3, 2)] Output : [] E
6 min read
Python | How to get unique elements in nested tuple Sometimes, while working with tuples, we can have a problem in which we have nested tuples and we need to extract elements that occur singly, i.e are elementary. This kind of problem can have applications in many domains. Let's discuss certain ways in which this problem can be solved. Method #1: Usi
7 min read
Find Unique Elements from Tuple in Python Tuples are immutable built-in data type in Python that can store multiple values in it. Extracting Unique Elements from a Tuple in Python can be done through two different approaches. Examples: Input: (1, 2, 13, 4, 3, 12, 5, 7, 7, 2, 2, 4)Output: (1, 2, 3,4,5,12,13)Input: ('Apple', 'Mango', 'Banana'
5 min read
Generating a "Set Of Tuples" from A "List of Tuples" - Python We are given a list of tuples and we need to extract only the unique tuples while removing any duplicates. This is useful in scenarios where you want to work with distinct elements from the list. For example:We are given this a list of tuples as [(1, 2), (3, 4), (1, 2), (5, 6)] then the output will
3 min read
Python - Unique Tuple Frequency (Order Irrespective) Given tuple list, extract the frequency of unique tuples in list order irrespective. Input : test_list = [(3, 4), (1, 2), (4, 3), (3, 4)] Output : 2 Explanation : (3, 4), (4, 3), (3, 4) makes 1 and (1, 2) is 2nd unique element. Input : test_list = [(3, 7), (1, 2), (4, 3), (5, 6)] Output : 4 Explanat
5 min read
Python - First K unique elements Sometimes, while working with Python Lists, we can have a problem in which we need to extract first K unique elements. This means we need to extract duplicate if they occur in first K elements as well. This can essentially make count of first K unique elements more than K. This kind of problem can h
3 min read