Python - Extend consecutive tuples
Last Updated :
10 Mar, 2023
Given list of tuples, join consecutive tuples.
Input : test_list = [(3, 5, 6, 7), (3, 2, 4, 3), (9, 4), (2, 3, 2)] Output : [(3, 5, 6, 7, 3, 2, 4, 3), (3, 2, 4, 3, 9, 4), (9, 4, 2, 3, 2)] Explanation : Elements joined with their consecutive tuples. Input : test_list = [(3, 5, 6, 7), (3, 2, 4, 3)] Output : [(3, 5, 6, 7, 3, 2, 4, 3)] Explanation : Elements joined with their consecutive tuples.
Method #1 : Using loop
This is brute way in which this task can be performed. In this, we perform task of joining consecution by access inside loop.
Python3
# Python3 code to demonstrate working of
# Extend consecutive tuples
# Using loop
# initializing list
test_list = [(3, 5, 6, 7), (3, 2, 4, 3), (9, 4), (2, 3, 2), (3, ), (3, 6)]
# printing original list
print("The original list is : " + str(test_list))
res = []
for idx in range(len(test_list) - 1):
# joining tuples
res.append(test_list[idx] + test_list[idx + 1])
# printing results
print("Joined tuples : " + str(res))
OutputThe original list is : [(3, 5, 6, 7), (3, 2, 4, 3), (9, 4), (2, 3, 2), (3, ), (3, 6)]
Joined tuples : [(3, 5, 6, 7, 3, 2, 4, 3), (3, 2, 4, 3, 9, 4), (9, 4, 2, 3, 2), (2, 3, 2, 3), (3, 3, 6)]
Time Complexity: O(n), where n is the length of the input list.
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the list “test_list”.
Method #2 : Using zip() + list comprehension
In this, we construct consecutive list using zip() and slicing and then form pairs accordingly.
Python3
# Python3 code to demonstrate working of
# Extend consecutive tuples
# Using zip() + list comprehension
# initializing list
test_list = [(3, 5, 6, 7), (3, 2, 4, 3), (9, 4), (2, 3, 2), (3, ), (3, 6)]
# printing original list
print("The original list is : " + str(test_list))
# zip to combine consecutive elements
res = [a + b for a, b in zip(test_list, test_list[1:])]
# printing results
print("Joined tuples : " + str(res))
OutputThe original list is : [(3, 5, 6, 7), (3, 2, 4, 3), (9, 4), (2, 3, 2), (3, ), (3, 6)]
Joined tuples : [(3, 5, 6, 7, 3, 2, 4, 3), (3, 2, 4, 3, 9, 4), (9, 4, 2, 3, 2), (2, 3, 2, 3), (3, 3, 6)]
Similar Reads
Python - Filter consecutive elements Tuples Given a Tuple list, filter tuples that are made from consecutive elements, i.e diff is 1. Input : test_list = [(3, 4, 5, 6), (5, 6, 7, 2), (1, 2, 4), (6, 4, 6, 3)] Output : [(3, 4, 5, 6)] Explanation : Only 1 tuple adheres to condition. Input : test_list = [(3, 4, 5, 6), (5, 6, 7, 2), (1, 2, 3), (6,
5 min read
Python | Arrange Tuples consecutively in list Sometimes, while working with tuple list, we may require a case in which we require that a tuple starts from the end of previous tuple, i.e the element 0 of every tuple should be equal to ending element of tuple in list of tuple. This type of problem and sorting is useful in competitive programming.
3 min read
tuple() Constructor in Python In Python, the tuple() constructor is a built-in function that is used to create tuple objects. A tuple is similar to a list, but it is immutable (elements can not be changed after creating tuples). You can use the tuple() constructor to create an empty tuple, or convert an iterable (such as a list,
2 min read
Python - Concatenate consecutive elements in Tuple Sometimes, while working with data, we can have a problem in which we need to find cumulative results. This can be of any type, product, or summation. Here we are gonna discuss adjacent element concatenation. Letâs discuss certain ways in which this task can be performed. Method #1 : Using zip() + g
4 min read
Python - Flatten Nested Tuples Sometimes, while working with Python Tuples, we can have a problem in which we need to perform flattening of tuples, which can be nested and undesired. This can have application across many domains such as Data Science and web development. Let's discuss certain way in which this task can be performe
7 min read