
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
How can I append a tuple into another tuple in Python?
In this article, we will show you how to append a tuple into another in python. Below are the various methods to accomplish this task −
Using + operator.
Using sum() function.
Using list() & extend() functions.
Using the unpacking(*) operator.
Tuples are an immutable, unordered data type used to store collections in Python. Lists and tuples are similar in many ways, but a list has a variable length and is mutable in comparison to a tuple which has a fixed length and is immutable.
Using + operator
Algorithm (Steps)
Following are the Algorithm/steps to be followed to perform the desired task −
Create a variable to store the input tuple 1.
Create another variable to store the input tuple 2.
Use the + operator to append or concat the 2nd tuple to the first tuple.
Print the resultant tuple after appending inputTuple_2 with the inputTuple_1.
Example
The following program appends inputTuple_2 with the inputTuple_1 using the + operator –
# 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)
Output
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
Algorithm (Steps)
Following are the Algorithm/steps to be followed to perform the desired task –.
Create a variable to store the input tuple 1.
Create another variable to store the input tuple 2.
Print both the given input tuples.
Pass both tuples as a tuple and an empty tuple as an argument to the sum() function(Returns the sum of all items in an iterable) to concatenate the given two tuples.
Print the resultant tuple after appending inputTuple_2 with the inputTuple_1.
Example
The following program appends inputTuple_2 with the inputTuple_1 using the sum() function −
# 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)
Output
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
Algorithm (Steps)
Following are the Algorithm/steps to be followed to perform the desired task –.
Create a variable to store the input tuple 1.
Create another variable to store the input tuple 2.
Print both the given input tuples.
Use the list() function(converts the sequence/iterable to a list) to convert both the input tuples into a list.
Use the extend() function(adds all the elements of an iterable like list, tuple, string etc to the end of the list) to append or concatenate the above 2nd list to the first list.
Use the tuple() function(create tuples in Python) to convert the resultant first list to a tuple which is the resultant tuple after appending the 2nd list to the 1st list.
Print the resultant tuple after appending.
Example
The following program appends inputTuple_2 with the inputTuple_1 using the list and extend() functions –
# 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)
Output
On executing, the above program will generate the following output −
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
Example
The following program appends inputTuple_2 with the inputTuple_1 using the unpacking operator –
# 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)
Output
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))
Conclusion
In this article, we learned four different Python methods for appending a tuple to another tuple. In this article, we also learned about the list(), extend(), and unpacking operator(*). In Python, we learned how to convert a given tuple to a list and a list to a tuple.
- Related Articles
- How I can convert a Python Tuple into Dictionary?
- How can I convert Python strings into tuple?
- How can I subtract tuple of tuples from a tuple in Python?
- How to append elements in Python tuple?
- How can I do Python Tuple Slicing?
- How can I use Multiple-tuple in Python?
- How we can use Python Tuple within a Tuple?
- How can I create a non-literal python tuple?
- How can I convert a Python tuple to string?
- How can I define duplicate items in a Python tuple?
- How I can dump a Python tuple in JSON format?
- How can I represent python tuple in JSON format?
- How can I create a Python tuple of Unicode strings?
- How can I remove items out of a Python tuple?
- How can I convert a Python tuple to an Array?
