
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How can I append a tuple into another tuple in Python?
In this article, we will demonstrate how to append one tuple to another in Python. Below are various methods to achieve this task -
-
Using + operator.
-
Using sum() function.
-
Using list() & extend() functions.
-
Using the unpacking(*) operator.
Tuples are an immutable, unordered data type used for storing collections in Python. While lists and tuples share many similarities, lists have a variable length and are mutable, whereas tuple have fixed length and remain immutable
Using + Operator
Defining a variable to store the first input tuple, is followed by another variable to hold the second input tuple. Then, use the + operator to append or concatenate the second tuple to the first. Finally, print the resulting tuple after the concatenation.
Example
The following program appends inputTuple_2 to inputTuple1 using the + operator. The operator concatenates them, creating resultTuple with the merged elements. The function then outputs the resulting tuple, displaying the combined sequence of numbers.
# input tuple 1 inputTuple_1 = (12, 8, 6) # input tuple 2 inputTuple_2 = (3, 4) # appending/concatenating 2nd tuple to the first tuple using the + operator resultTuple = inputTuple_1 + inputTuple_2 # printing the resultant tuple after appending print("Resultant tuple after appending inputTuple_2 to the inputTuple_1:\n", resultTuple)
On executing, the above program will generate the following output ?
Resultant tuple after appending inputTuple_2 to the inputTuple_1: (12, 8, 6, 3, 4)
Using sum() Function
We are defining a variable to store the first input tuple, followed by another variable to hold the second input tuple. Print both tuples to display their initial values. Then, use the sum() function, passing both tuples along with an empty tuple as arguments to concatenate them. Finally, print the resulting tuple after merging inputTuple_2 with inputTuple_1.
Example
This Python code defines two tuples: inputTuple_1 and inpuTuplr_2. It prints their values and then concatenates them using the sum() function, passing an empty tuple as the initial value. the result, resultYuple, contains merged elements ans is printed as output.
# input tuple 1 inputTuple_1 = (12, 8, 6) # input tuple 2 inputTuple_2 = (3, 4) # printing both the given input tuples print("inputTuple_1: ", inputTuple_1) print("inputTuple_2: ", inputTuple_2) # appending/concatenating 2nd tuple to the first tuple resultTuple = sum((inputTuple_1, inputTuple_2), ()) # printing the resultant tuple after appending print("Resultant tuple after appending inputTuple_2 to the inputTuple_1:\n", resultTuple)
On executing, the above program will generate the following output ?
inputTuple_1: (12, 8, 6) inputTuple_2: (3, 4) Resultant tuple after appending inputTuple_2 to the inputTuple_1: (12, 8, 6, 3, 4)
Using list() & extend() Functions
This defines the variables to store the input tuples and prints them. Converts both tuples into lists using list(), then use extend() to merge the second list into the first. Finally, convert the merged list back into a tuple using tuple() and print the result.
Example
This Python code defines two tuples, inputTuple_1 and inputTuple2. It prints them, converts both into lists, and merges them using extend(). The combined list is then converted back into a tuple. Finally, the merged resultTuple is printed as output.
# input tuple 1 inputTuple_1 = (12, 8, 6) # input tuple 2 inputTuple_2 = (3, 4) # printing both the given input tuples print("inputTuple_1: ", inputTuple_1) print("inputTuple_2: ", inputTuple_2) # converting inputTuple_1 into list list_1 = list(inputTuple_1) # converting inputTuple_2 into list list_2 = list(inputTuple_2) # appending/concatenating 2nd list to the first list list_1.extend(list_2) # converting the resultant first list to a tuple # which is the resultant tuple after appending the 2nd list to the 1st list resultTuple=tuple(list_1) # printing the resultant tuple after appending print("Resultant tuple after appending inputTuple_2 to the inputTuple_1:\n", resultTuple)
The result is obtained as follows -
inputTuple_1: (12, 8, 6) inputTuple_2: (3, 4) Resultanat tuple after appending inputTuple_2 to the inputTuple_1: (12, 8, 6, 3, 4)
Using the unpacking (*) operator
The unpacking * operator in Python tuples extracts individual elements from a tuple. It can be used to pass elements as arguments, combine tuples, or collect remaining items in assignments. It determines the flexibility, especially when working with variable-length tuples or simplifying tuple manipulations in functions or loops.
Example
We are defining two tuples that prints and it impacts while appending inputTuple_2 as a nested tuple, storing the result in resultTuple. FInally, the modified tuple structure is printed as output.
# input tuple 1 inputTuple_1 = (12, 8, 6) # input tuple 2 inputTuple_2 = (3, 4) # printing both the given input tuples print("inputTuple_1: ", inputTuple_1) print("inputTuple_2: ", inputTuple_2) # Unpacking the first tuple and adding the second tuple resultTuple= (*inputTuple_1,inputTuple_2) # Printing the result tuple # printing the resultant tuple after appending print("Resultant tuple after appending inputTuple_2 to the inputTuple_1:\n", resultTuple)
We will get the output as follows -
inputTuple_1: (12, 8, 6) inputTuple_2: (3, 4) Resultant tuple after appending inputTuple_2 to the inputTuple_1: (12, 8, 6, (3, 4))