# Python program to record similar tuple occurrence

In this problem, we are given a list of tuples consisting of integer values and duplicate tuple values. Our task is to create a Python program to record similar tuple occurrence.
Submitted by Shivang Yadav, on January 16, 2022

Filtering duplicate records from tuples before processing is very important in Python's implementation. Python programming language provides tools to perform such tasks. Here, we will see a Python program to record similar tuple occurrences.

Before going further with the problem, let's recap some basic topics that will help in understanding the solution.

Python programming language is a high-level and object-oriented programming language. Python is an easy to learn, powerful high-level programming language. It has a simple but effective approach to object-oriented programming.

Tuples in Python is a collection of items similar to list with the difference that it is ordered and immutable.

Example:

```tuple = ("python", "includehelp", 43, 54.23)
```

## Recording Similar Tuple Occurrence

We are given a list of tuples with integer values where duplicate tuples are also present. We need to create a Python program to record similar tuple occurrence i.e. count the number of times a tuple has occurred.

```Input:
tupList = [(9, 5), (0, 2), (3, 3), (9, 5), (3, 3)]

Output:
[(9, 5) : 2, (0, 2) : 1, (3, 3) : 2]
```

To perform this task, we will iterate over the list and count the occurrence of each tuple in the list and return a list with tuple and occurrence count. Python provides tools and methods to perform this task in a simple manner.

Method 1:

One method to solve the problem is by creating s dictionary that contains the occurrence count of the tuple. For this, we will iterate over the list which is a sorted form, and use a counter to count distinct occurrences. Then return the distinct value dictionary.

```# Python program to record similar tuple
# occurrence using Counter

from collections import Counter

# Initializing and printing list to tuples
tupList = [(9, 5), (0, 2), (3, 3), (9, 5), (3, 3)]
print("The elements of list of tuples is " + str(tupList))

# Recording similar tuple occurrence
similarDict = dict(Counter(tuple(val) for val in map(sorted, tupList)))

# Printing result
print("The occurrence count of each tuple in list is " + str(similarDict))
```

Output:

```The elements of list of tuples is [(9, 5), (0, 2), (3, 3), (9, 5), (3, 3)]
The occurrence count of each tuple in list is {(5, 9): 2, (0, 2): 1, (3, 3): 2}
```

Method 2:

Another approach includes the use of frozenset() method that orders the tuple. This makes sure that the counting done using the counter() method is more effective.

```# Python program to record similar
# tuple occurrence

from collections import Counter

# Initializing and printing list to tuples
tupList = [(9, 5), (0, 2), (3, 3), (9, 5), (3, 3)]
print("The elements of list of tuples is " + str(tupList))

# Recording similar tuple occurrence
similarDict = dict(Counter(tuple(frozenset(tup)) for tup in tupList))

# Printing result
print("The occurrence count of each tuple in list is " + str(similarDict))
```

Output:

```The elements of list of tuples is [(9, 5), (0, 2), (3, 3), (9, 5), (3, 3)]
The occurrence count of each tuple in list is {(9, 5): 2, (0, 2): 1, (3,): 2}
```

Preparation

What's New

Top Interview Coding Problems/Challenges!