Open In App

Python | Find Dissimilar Elements in Tuples

Last Updated : 21 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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




# Python3 code to demonstrate working of
# Dissimilar elements in tuples
# Using set() + "^" operator
 
# initialize tuples
test_tup1 = (3, 4, 5, 6)
test_tup2 = (5, 7, 4, 10)
 
# printing original tuples
print("The original tuple 1 : " + str(test_tup1))
print("The original tuple 2 : " + str(test_tup2))
 
# Dissimilar elements in tuples
# Using set() + "^" operator
res = tuple(set(test_tup1) ^ set(test_tup2))
 
# printing result
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




# Python3 code to demonstrate working of
# Dissimilar elements in tuples
# Using symmetric_difference() + set()
 
# initialize tuples
test_tup1 = (3, 4, 5, 6)
test_tup2 = (5, 7, 4, 10)
 
# printing original tuples
print("The original tuple 1 : " + str(test_tup1))
print("The original tuple 2 : " + str(test_tup2))
 
# Dissimilar elements in tuples
# Using symmetric_difference() + set()
res = tuple(set(test_tup1).symmetric_difference(set(test_tup2)))
 
# printing result
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




# Python3 code to demonstrate working of
# Dissimilar elements in tuples
 
# initialize tuples
test_tup1 = (3, 4, 5, 6)
test_tup2 = (5, 7, 4, 10)
 
# printing original tuples
print("The original tuple 1 : " + str(test_tup1))
print("The original tuple 2 : " + str(test_tup2))
 
# Dissimilar elements in tuples
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)
# printing result
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




# Python3 code to demonstrate working of
# Dissimilar elements in tuples
# Using filter() and lambda function
 
# initialize tuples
test_tup1 = (3, 4, 5, 6)
test_tup2 = (5, 7, 4, 10)
 
# printing original tuples
print("The original tuple 1 : " + str(test_tup1))
print("The original tuple 2 : " + str(test_tup2))
 
# Dissimilar elements in tuples
# Using filter() and lambda function
res = tuple(filter(lambda x: x not in test_tup2, test_tup1)) + \
    tuple(filter(lambda x: x not in test_tup1, test_tup2))
 
# printing result
print("The Dissimilar elements from tuples are : " + str(res))
# This code is contributed by Edula Vinay Kumar Reddy


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))
 
 
#This code is contributed by Jyothi pinjala.


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




# Python3 code to demonstrate working of
# Dissimilar elements in tuples
 
# initialize tuples
test_tup1 = (3, 4, 5, 6)
test_tup2 = (5, 7, 4, 10)
 
# printing original tuples
print("The original tuple 1 : " + str(test_tup1))
print("The original tuple 2 : " + str(test_tup2))
 
# Dissimilar elements in tuples
dissimilar_elements = tuple(set(test_tup1).difference(test_tup2)) + tuple(set(test_tup2).difference(test_tup1))
 
# printing result
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




# Python3 code to demonstrate working of
# Dissimilar elements in tuples
# Using set() + "-" operator
 
# initialize tuples
test_tup1 = (3, 4, 5, 6)
test_tup2 = (5, 7, 4, 10)
 
# printing original tuples
print("The original tuple 1 : " + str(test_tup1))
print("The original tuple 2 : " + str(test_tup2))
 
# Dissimilar elements in tuples
# Using set() + "-" operator
res = tuple(set(test_tup1) - set(test_tup2)) + tuple(set(test_tup2) - set(test_tup1))
 
# printing result
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)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads