Signup/Sign In
PUBLISHED ON: JULY 15, 2021

Program Adding Tuple to List and vice versa

In this tutorial, we will learn to write a Python program where we have to add a tuple to a list and vice-versa. We have to perform the addition of a data structure in another data structure of Python. A tuple is immutable while a list is a mutable data structure. To know more about lists and tuples and their conversion read this article.

Look at the examples to understand the input and output format.

Input:

list=[12, 10, 11, 3]

tuple= (2, 5)

Output: [12, 10, 11, 3, 2, 5]

Input:

list=[9, 5, 2, 1]

tuple=(13, 14)

Output: (13, 14, 9, 5, 2, 1)

To solve this problem in python, we can use the following approaches-

  1. using += operator for lists and tuples
  2. using tuple() and list() method

We will discuss both approaches in detail below.

Approach 1: Using += operator in Python

In this approach, we will use a special operator to join lists and tuples in Python. The += operator works by joining both the data structures together.

Algorithm

Follow the algorithm to understand the approach better.

Step 1- Initialise a list

Step 2- Initialise a tuple

Step 3- Print list and tuple

Step 4- Add tuple to list

Step 5- Print the list after adding tuple as output

Python Program 1

In this program, we have added a tuple to a list. The operator will join the two data structures together. The result will be a list.

li = [5, 6, 7]
tupl= (9, 10)
print("List: ", li)
print("Tuple: ", tupl)
li += tupl
print("After adding: ",li)


List: [5, 6, 7]
Tuple: (9, 10)
After adding: [5, 6, 7, 9, 10]

Approach 2: list() and tuple()

In this approach, we will use the list() and tuple() methods for type conversion. We will add a tuple to a list in this example. First, we will convert the tuple to a list and then concatenate both the lists together using (+). Then we will convert the final list to a tuple.

Algorithm

Follow the algorithm to understand the approach better.

Step 1- Initialise a list

Step 2- Initialise a tuple

Step 3- Print list and tuple

Step 4- Declare a variable to store the result

Step 5- Convert tuple to list and join both lists

Step 6- Convert final list to tuple and display as an output

Python Program 2

In this program, we have declared a list and a tuple and added the tuple to a list. We have used built-in methods for type conversion.

li = [1,2,3]
tupl= (4, 5)
print("List: ", li)
print("Tuple: ", tupl)
result= tuple(list(tupl) + li)
print("After adding: ",result)


List: [1, 2, 3]
Tuple: (4, 5)
After adding: (4, 5, 1, 2, 3)

Conclusion

In this tutorial, we have learned how to add a tuple to a list and vice-versa in Python. We have discussed two different approaches to do this program. In the first approach, we have used a special operator to join the list and tuple. In the second approach, we have used list() and tuple() methods for conversion.



About the author:
Nikita Pandey is a talented author and expert in programming languages such as C, C++, and Java. Her writing is informative, engaging, and offers practical insights and tips for programmers at all levels.