Python | Find Dissimilar Elements in Tuples
Last Updated :
21 Mar, 2023
Sometimes, while working with tuples, we can have a problem in which we need dissimilar features of two records. This type of application can come in Data Science domain. Let’s discuss certain ways in which this problem can be solved.
Method #1 : Using set() + “^” operator This task can be performed using symmetric difference functionality offered by XOR operator over sets. The conversion to set is done by set().
Python3
test_tup1 = ( 3 , 4 , 5 , 6 )
test_tup2 = ( 5 , 7 , 4 , 10 )
print ( "The original tuple 1 : " + str (test_tup1))
print ( "The original tuple 2 : " + str (test_tup2))
res = tuple ( set (test_tup1) ^ set (test_tup2))
print ( "The Dissimilar elements from tuples are : " + str (res))
|
Output
The original tuple 1 : (3, 4, 5, 6)
The original tuple 2 : (5, 7, 4, 10)
The Dissimilar elements from tuples are : (3, 6, 7, 10)
Time complexity: O(n), where n is the length of the longer tuple (as set() and ^ operator take linear time).
Auxiliary space: O(n), where n is the length of the longer tuple (as we are creating a set of the elements in the tuples).
Method #2 : Using symmetric_difference() + set() This is method similar to above method, the difference is that instead of XOR operator, we use inbuilt function to perform the task of filtering dissimilar elements.
Python3
test_tup1 = ( 3 , 4 , 5 , 6 )
test_tup2 = ( 5 , 7 , 4 , 10 )
print ( "The original tuple 1 : " + str (test_tup1))
print ( "The original tuple 2 : " + str (test_tup2))
res = tuple ( set (test_tup1).symmetric_difference( set (test_tup2)))
print ( "The Dissimilar elements from tuples are : " + str (res))
|
Output
The original tuple 1 : (3, 4, 5, 6)
The original tuple 2 : (5, 7, 4, 10)
The Dissimilar elements from tuples are : (3, 6, 7, 10)
Time complexity: O(n), where n is the length of the input list.
Auxiliary space: O(1), as the space used by the algorithm is constant and does not depend on the size of the input.
Method #3: Using in, not in operators and tuple() methods
Step-by-step approach:
- Two tuples are initialized test_tup1 and test_tup2 with values
- The original tuples are printed using the print() function.
- An empty list x is initialized.
- A for loop is used to iterate through each element i in test_tup1.
- For each i, the if condition checks whether i is present in test_tup2 or not using the not in operator.
- If i is not present in test_tup2, then it is appended to the list x using the append() method.
- Another for loop is used to iterate through each element i in test_tup2.
- For each i, the if condition checks whether i is present in test_tup1 or not using the not in operator.
- If i is not present in test_tup1, then it is appended to the list x using the append() method.
- The list x is converted to a tuple using the tuple() function.
- The Dissimilar elements from tuples are printed using the print() function.
Below is the implementation of the above approach:
Python3
test_tup1 = ( 3 , 4 , 5 , 6 )
test_tup2 = ( 5 , 7 , 4 , 10 )
print ( "The original tuple 1 : " + str (test_tup1))
print ( "The original tuple 2 : " + str (test_tup2))
x = []
for i in test_tup1:
if i not in test_tup2:
x.append(i)
for i in test_tup2:
if i not in test_tup1:
x.append(i)
x = tuple (x)
print ( "The Dissimilar elements from tuples are : " + str (x))
|
Output
The original tuple 1 : (3, 4, 5, 6)
The original tuple 2 : (5, 7, 4, 10)
The Dissimilar elements from tuples are : (3, 6, 7, 10)
Time complexity: O(n^2), where n is the length of the larger tuple
Auxiliary space: O(n), where n is the length of the resulting tuple
Method #4 : Using filter() and lambda function
Note that this method is similar to the third method, but instead of using for loops and the in operator, it uses the filter() function and a lambda function. The filter() function returns an iterator that only yields the elements of an iterable (in this case, the original tuple) for which the lambda function returns True. In this case, the lambda function checks if an element is not in the other tuple. The result of the filter() function for each tuple is then combined using the + operator.
Python3
test_tup1 = ( 3 , 4 , 5 , 6 )
test_tup2 = ( 5 , 7 , 4 , 10 )
print ( "The original tuple 1 : " + str (test_tup1))
print ( "The original tuple 2 : " + str (test_tup2))
res = tuple ( filter ( lambda x: x not in test_tup2, test_tup1)) + \
tuple ( filter ( lambda x: x not in test_tup1, test_tup2))
print ( "The Dissimilar elements from tuples are : " + str (res))
|
Output
The original tuple 1 : (3, 4, 5, 6)
The original tuple 2 : (5, 7, 4, 10)
The Dissimilar elements from tuples are : (3, 6, 7, 10)
Time complexity: O(n), where n is the number of elements in the tuple. This is because the filter() function iterates through the entire tuple and applies the lambda function to each element, which takes constant time.
Auxiliary space: O(n), as it creates a new list to store the dissimilar elements from the two tuples. The size of this list would be equal to the number of dissimilar elements, which can be at most n, where n is the number of elements in the tuple. Therefore, the space complexity is O(n).
Method #5 : Using List comprehension
Python3
test_tup1 = ( 3 , 4 , 5 , 6 )
test_tup2 = ( 5 , 7 , 4 , 10 )
print ( "The original tuple 1 : " + str (test_tup1))
print ( "The original tuple 2 : " + str (test_tup2))
dissimilar = tuple ( sorted ( set ([i for i in test_tup1 if i not in test_tup2] + [i for i in test_tup2 if i not in test_tup1])))
print ( "The Dissimilar elements from tuples are : " + str (dissimilar))
|
Output
The original tuple 1 : (3, 4, 5, 6)
The original tuple 2 : (5, 7, 4, 10)
The Dissimilar elements from tuples are : (3, 6, 7, 10)
Time complexity: O(m+n)
Auxiliary space: O(m+n) , m and n are the sizes of the two tuples.
Method #6: Approach that uses the difference method of the set data structure:
In this approach, we first convert the tuples into sets using the set constructor. Then, we use the difference method to find the elements in one set that are not in the other set. Finally, we concatenate the results of the two difference operations using the + operator, and convert the result to a tuple.
Python3
test_tup1 = ( 3 , 4 , 5 , 6 )
test_tup2 = ( 5 , 7 , 4 , 10 )
print ( "The original tuple 1 : " + str (test_tup1))
print ( "The original tuple 2 : " + str (test_tup2))
dissimilar_elements = tuple ( set (test_tup1).difference(test_tup2)) + tuple ( set (test_tup2).difference(test_tup1))
print ( "The Dissimilar elements from tuples are : " + str (dissimilar_elements))
|
Output
The original tuple 1 : (3, 4, 5, 6)
The original tuple 2 : (5, 7, 4, 10)
The Dissimilar elements from tuples are : (3, 6, 7, 10)
Time complexity: O(n), where n is the length of the longest tuple.
Auxiliary space: O(n)
Method #7: Using set() + “-” operator
This method uses the set difference operator to find the elements that are present in one tuple but not in the other.
Python3
test_tup1 = ( 3 , 4 , 5 , 6 )
test_tup2 = ( 5 , 7 , 4 , 10 )
print ( "The original tuple 1 : " + str (test_tup1))
print ( "The original tuple 2 : " + str (test_tup2))
res = tuple ( set (test_tup1) - set (test_tup2)) + tuple ( set (test_tup2) - set (test_tup1))
print ( "The Dissimilar elements from tuples are : " + str (res))
|
Output
The original tuple 1 : (3, 4, 5, 6)
The original tuple 2 : (5, 7, 4, 10)
The Dissimilar elements from tuples are : (3, 6, 10, 7)
Time complexity: O(n), where n is the length of the longest tuple.
Auxiliary space: O(n)
Similar Reads
Find an Element In a List of Tuples
In Python programming, it is often necessary to locate an element inside a list of tuples. Tuples are arranged collections, and locating a particular piece inside them requires knowledge of a variety of strategies. When data is kept as tuples inside a list, this procedure is essential for data retri
3 min read
Python - Tuple elements inversions
Sometimes, while programming, we have a problem in which we might need to perform certain bitwise operations among tuple elements. This is an essential utility as we come across bitwise operations many times. Letâs discuss certain ways in which this task can be performed. Method #1 : Using map() + l
3 min read
Python | Binary Group Tuple list elements
Sometimes, while working with tuples, we can have problems of grouping them, be it based on gender or any particular binary category. This can have applications in many domains. Let's discuss certain ways in which this can be performed. Method #1 : Using generator + loop + zip() The brute force meth
4 min read
Python - Extract tuples having K digit elements
Given a list of tuples, extract all tuples having K digit elements. Input : test_list = [(54, 2), (34, 55), (222, 23), (12, 45), (78, )], K = 2 Output : [(34, 55), (12, 45), (78,)] Explanation : All tuples have numbers with 2 digits. Input : test_list = [(54, 2), (34, 55), (222, 23), (12, 45), (782,
6 min read
Python - Filter consecutive elements Tuples
Given a Tuple list, filter tuples that are made from consecutive elements, i.e diff is 1. Input : test_list = [(3, 4, 5, 6), (5, 6, 7, 2), (1, 2, 4), (6, 4, 6, 3)] Output : [(3, 4, 5, 6)] Explanation : Only 1 tuple adheres to condition. Input : test_list = [(3, 4, 5, 6), (5, 6, 7, 2), (1, 2, 3), (6,
5 min read
Getting an Element from Tuple of Tuples in Python
Tuples in Python are versatile data structures that allow you to store and organize collections of elements. When dealing with tuples of tuples, accessing specific elements becomes essential for effective data manipulation. In this article, we'll explore some different methods to retrieve elements f
2 min read
Python - Group Tuples by Kth Index Element
Sometimes, while working with Python records, we can have a problem in which we need to perform grouping of elements of tuple by similar Kth index element. This kind of problem can have application in web development domain. Let's discuss the certain way in which this task can be performed. Input :
5 min read
Python - Pair lists elements to Dictionary
Sometimes, while working with records we can have problems in which we can have pair of lists, we need to pair similar elements to a single key-value dictionary. This is a very peculiar problem but can have applications in data domains. Let us discuss certain ways in which this task can be performed
6 min read
Python - Join Tuples if similar initial element
Sometimes, while working with Python tuples, we can have a problem in which we need to perform concatenation of records from the similarity of initial element. This problem can have applications in data domains such as Data Science. Let's discuss certain ways in which this task can be performed. Inp
8 min read
Python - Check if Tuple contains only K elements
Sometimes, while working with Python tuples, we can have a problem in which we need to test if any tuple contains elements from set K elements. This kind of problem is quite common and have application in many domains such as web development and day-day programming. Let's discuss certain ways in whi
3 min read