Python | Records Intersection
Last Updated :
06 Apr, 2023
Sometimes, while working with tuples, we can have a problem in which we need similar features of two records. This type of application can come in the 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
# Records Intersection
# 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))
# Records Intersection
# Using set() + "&" operator
res = tuple(set(test_tup1) & set(test_tup2))
# printing result
print("The similar elements from tuples are : " + str(res))
Output : The original tuple 1 : (3, 4, 5, 6)
The original tuple 2 : (5, 7, 4, 10)
The similar elements from tuples are : (4, 5)
Method #2: Using intersection() + set() This is a method similar to the above method, the difference is that instead of & operator, we use inbuilt function to perform the task of filtering dissimilar elements.
Python3
# Python3 code to demonstrate working of
# Records Intersection
# Using intersection() + 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))
# Records Intersection
# Using intersection() + set()
res = tuple(set(test_tup1).intersection(set(test_tup2)))
# printing result
print("The similar elements from tuples are : " + str(res))
Output : The original tuple 1 : (3, 4, 5, 6)
The original tuple 2 : (5, 7, 4, 10)
The similar elements from tuples are : (4, 5)
Time complexity: O(n), where n is the length of the longer tuple because we are using the intersection() method of sets to find the common elements.
Auxiliary space: O(n) because we are creating two sets, each of which contains the same number of elements as the corresponding tuple, and a third set to store the common elements.
Method #3: Using Counter
Python3
from collections import Counter
# 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))
# Records Intersection
# Using Counter
res = tuple(x for x in test_tup1 if Counter(
test_tup1)[x] == Counter(test_tup2)[x])
# printing result
print("The similar elements from tuples are : " + str(res))
# This code is contributed by Edula Vinay Kumar Reddy
OutputThe original tuple 1 : (3, 4, 5, 6)
The original tuple 2 : (5, 7, 4, 10)
The similar elements from tuples are : (4, 5)
This approach uses the Counter class from the collections module to create a counter object for each tuple, and then uses a list comprehension to filter the elements of the first tuple that have the same count in both counter objects. It then converts the resulting list to a tuple. Time complexity is O(n) as it iterates through the first tuple once and creates two counter objects with a time complexity of O(n) each.
Method #4: Using list comprehension and the "in" operator
Use a list comprehension and the "in" operator to find the common elements between the two tuples.
- Create a tuple named test_tup1 containing the values (3, 4, 5, 6).
- Create another tuple named test_tup2 containing the values (5, 7, 4, 10).
- Use a list comprehension to create a new tuple named res containing the elements that are common to both test_tup1 and test_tup2.
- Iterate through each element x in test_tup1.
- Check if x is in test_tup2.
- If x is in test_tup2, add it to the res tuple.
- Convert the res tuple to a string and print it, along with a message stating that it contains the similar elements from the tuples.
Python3
# initialize tuples
test_tup1 = (3, 4, 5, 6)
test_tup2 = (5, 7, 4, 10)
# find common elements
res = tuple(x for x in test_tup1 if x in test_tup2)
# printing result
print("The similar elements from tuples are : " + str(res))
OutputThe similar elements from tuples are : (4, 5)
Time complexity: O(n*m), where n and m are the lengths of the two input tuples.
Auxiliary space: O(k), where k is the number of elements that are common to both tuples.
Method 5: Using built-in function filter() + lambda function
The filter() function takes two arguments, a lambda function and an iterable, and returns a filter object containing only the items from the iterable for which the lambda function returns True. In this case, the lambda function checks if the element of test_tup1 is also present in test_tup2.
- Two tuples test_tup1 and test_tup2 are initialized with some elements.
- The original tuples test_tup1 and test_tup2 are printed using the print() function and string concatenation.
- A new tuple res is created by applying the filter() function on test_tup1. The filter() function is passed a lambda function as its first argument which checks if the element of test_tup1 is also present in test_tup2. The filter() function returns a filter object containing only the common elements between test_tup1 and test_tup2. The tuple() function is applied on this filter object to convert it into a new tuple.
- The new tuple res containing the common elements between test_tup1 and test_tup2 is printed using the print() function and string concatenation.
- The program execution ends.
Python3
# 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))
# Records Intersection using filter() and lambda function
res = tuple(filter(lambda x: x in test_tup2, test_tup1))
# printing result
print("The similar elements from tuples are : " + str(res))
OutputThe original tuple 1 : (3, 4, 5, 6)
The original tuple 2 : (5, 7, 4, 10)
The similar elements from tuples are : (4, 5)
Time complexity: This approach has a time complexity of O(n), where n is the length of the smaller tuple.
Auxiliary space: This approach requires O(n) space to store the common elements in a new tuple.
Similar Reads
Python | Intersection in Tuple Records Data
Sometimes, while working with data, we may have a problem in which we require to find the matching records between two lists that we receive. This is a very common problem and records usually occur as a tuple. Let's discuss certain ways in which this problem can be solved. Method #1 : Using list com
7 min read
Python - Rows intersection with K
Sometimes while working with Python Matrix, we can have a problem in which we need to extract all the rows which match with other matrix, which has a specific element. This kind of problem can occur in data domains as Matrix are input data types for many problems. Let's discuss certain ways in which
3 min read
Multiple Sets Intersection in Python
In this article, a List of sets is given our task is to write a program to perform their intersection using Python. Examples of finding the intersection of multiple setsInput : test_list = [{5, 3, 6, 7}, {1, 3, 5, 2}, {7, 3, 8, 5}, {8, 4, 5, 3}] Output : {3, 5} Explanation : 3 and 5 is present in al
2 min read
Python | Difference in Record Lists
Sometimes, while working with data, we may have a problem in which we require to find the difference records between two lists that we receive. This is a very common problem and records usually occurs as a tuple. Letâs discuss certain ways in which this problem can be solved. Method #1 : Using list
5 min read
Python - Remove None Nested Records
Sometimes, while working with Python Records, can have problem in which we need to perform the removal of data which have all key's values as None in nested records. This kind of problem can have application in data preprocessing. Lets discuss certain ways in which this task can be performed. Method
4 min read
Python - Paired Existence in Records
Sometimes, while working with Python records, we can have a problem in which we need to check for paired existence inside the record, or else if one doesn't exist, other also should not. This kind of problem is common in domains such as Data Science and web development. Let's discuss certain ways in
7 min read
Python | Mutually different Records
Sometimes, while working with data, we may have a problem in which we require to find the unmatching records between two lists that we receive. This is a very common problem and records usually occurs as a tuple. Letâs discuss certain ways in which this problem can be solved. Method #1 : Using list
4 min read
Binary Search (bisect) in Python
Binary Search is a technique used to search element in a sorted list. In this article, we will looking at library functions to do Binary Search.Finding first occurrence of an element. bisect.bisect_left(a, x, lo=0, hi=len(a)) : Returns leftmost insertion point of x in a sorted list. Last two paramet
2 min read
Concatenate All Records - Python
The task of concatenating all records in Python involves combining elements from a list, typically strings, into a single unified string. The goal is to concatenate each individual element, ensuring that the result is a continuous string without spaces or delimiters, unless specified. For example, g
3 min read
Python - Occurrence counter in List of Records
Sometimes, while dealing with records we can have a problem in which we need count the occurrence of incoming digits corresponding to different characters/players in a game and compile them in a dictionary. This can have application in gaming and web development. Lets discuss a way in which this can
2 min read