
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Find Dissimilar Elements in Tuples in Python
When it is required to find dissimilar elements in tuples, the 'set' operator and the '^' operator can be used.
Python comes with a datatype known as 'set'. This 'set' contains elements that are unique only.
The set is useful in performing operations such as intersection, difference, union and symmetric difference.
The '^' operator is a bitwise operator that performs the 'XOR' operation. It sets every bit to 1 if only one of the two bits is 1.
Below is a demonstration of the same −
Example
my_tuple_1 = ((7, 8), (3, 4), (3, 2)) my_tuple_2 = ((9, 6), (8, 2), (1, 4)) print ("The first tuple is : " ) print(my_tuple_1) print ("The second tuple is : " ) print(my_tuple_2) my_result = tuple(set(my_tuple_1) ^ set(my_tuple_2)) print("The dissimilar elements in the tuples are : ") print(my_result)
Output
The first tuple is : ((7, 8), (3, 4), (3, 2)) The second tuple is : ((9, 6), (8, 2), (1, 4)) The dissimilar elements in the tuples are : ((3, 4), (9, 6), (1, 4), (8, 2), (3, 2), (7, 8))
Explanation
- Two nested tuples/tuple of tuples are defined and are displayed on the console.
- The '^' operator is used to find the elements that are not similar to one other.
- This result is assigned to a variable.
- It is displayed as output on the console.
- Related Articles
- Python program to find Tuples with positive elements in List of tuples
- Python program to find Tuples with positive elements in a List of tuples
- Python – Extract tuples with elements in Range
- Trim tuples by N elements in Python
- Find top K frequent elements from a list of tuples in Python
- Extract tuples having K digit elements in Python
- Python – Filter consecutive elements Tuples
- Python program to find tuples which have all elements divisible by K from a list of tuples
- Combining tuples in list of tuples in Python
- Python - Change the signs of elements of tuples in a list
- Count tuples occurrence in list of tuples in Python
- Find the tuples containing the given element from a list of tuples in Python
- Python program to convert elements in a list of Tuples to Float
- Updating Tuples in Python
- Compare tuples in Python

Advertisements