Dark Mode On/Off
PUBLISHED ON: AUGUST 19, 2021

# Python Program To Join Tuples If Similar Initial Element

A tuple is one of Python's four built-in data types for storing data collections; the other three are List, Set, and Dictionary, each having its own set of properties and applications.

In this tutorial, we will join the tuples if a similar initial element is found with two different methods.

### Need to Join Tuples

You could come into instances in programming when a collection contains tuples with identical starting components. In this case, we must merge all of these tuples into a single one, with a single starting element followed by further components in sequential order.

In data science, this is commonly done during data cleansing.

At first, Look at the example to understand the input-output format:

Input : List = [(1, 2), (1, 3), (1, 4), (5, 6), (7, 8)]

Output: [(1, 2, 3, 4), (5, 6), (7, 8)]

Input: List_1= [(9, 10), (11, 12), (11, 13), (11, 14), (15, 16)]

Output = [(9, 10),(11, 12, 13, 14), (15, 16)]

We may use different types of techniques to do this task, some of which are mentioned below:

2. ### Using defaultdict Method

We will discuss all the above methods one by one in detail separately.

### Approach 1: Brute Force Method (Using Loops)

The problem can be solved simply by iterating over the list and checking if the first element of each tuple exists in another tuple. If so, we can use the extended method to add it to the previous one. Otherwise, we'll add a new tuple to the list and finish the loop. The tuple list will be printed.

### Algorithm

1. A tuple list is created.
2. There is a definition for an empty list.
3. The iteration of the tuple list is tested to verify if the beginning items match.
4. The element is kept in the empty list if they match.
5. Otherwise, it is transformed to a tuple, then to a list, and finally to a list, before being put in the empty list.

### Program

In this program, we have already considered the input in the code itself such that the output will be directed on that input only.

``````List = [(1, 2), (1, 3), (1, 4), (5, 6), (7, 8)]

print("The original list is: ")
print(List)

result = []
for sub in List:
if result and result[-1][0] == sub[0]:
result[-1].extend(sub[1:])
else:
result.append([ele for ele in sub])
result = list(map(tuple, result))

print("The joined list is: " )
print(result)``````

The original list is:
[(1, 2), (1, 3), (1, 4), (5, 6), (7, 8)]
The joined list is:
[(1, 2, 3, 4), (5, 6), (7, 8)]

### Approach 2: Using defaultdict Method(With Loops)

defaultdict is a particular form of dictionary that handles 'key error' by giving non-existing keys a default value. All of the values from our list of tuples will be inserted into the default dictionary with a list as the value. Then, for the resultant tuple list, we'll extract each key-value pair and convert it to a tuple.

### Algorithm

1. Import deafaultdict library
2. Initializing List
3. Print the List
4. Join tuples if the similar initial element using defaultdict
5. Print the result

### Program

In this program, we have already considered the input in the code itself such that the output will be directed on that input only.

This problem can be solved using a mix of defaultdict and loops. It reduces the number of checks which is required to generate a key. It also works with non-sequential items.

``````from collections import defaultdict

# initializing list
List = [(9, 10), (11, 12), (11, 13), (11, 14), (15, 16)]

# original list
print("The original list is : " + str(List))

# Using defaultdict() with loop
ddl = defaultdict(list)
for key, val in List:
ddl[key].append(val)
result = [(key, *val) for key, val in ddl.items()]

#  result
print("The list after joining : " + str(result))``````

The Original List is: [(9, 10), (11, 12), (11, 13), (11, 14), (15, 16)]
The list after joining : [(9, 10),(11, 12, 13, 14), (15, 16)]

### Conclusion

In this tutorial, we have seen two approaches to join tuples if the initial element is similar. The first approach is the brute-force method which means it can be done with help of loops and the second approach is by using the defaultdict method with loops.

Want to learn coding?
Try our new interactive courses.
Over 20,000+ students enrolled.