 Open in App
Not now

# Python | Records Intersection

• Last Updated : 31 Jan, 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 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 method similar to 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)```

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`

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)```

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.

My Personal Notes arrow_drop_up