Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Adding Tuple to List and vice versa in Python
In Python, both tuples and lists are used to store data in sequence. Python provides built-in functions and operators that allow us to easily add elements of a tuple into a list and elements of a list into a tuple.
In this article, we will explain various methods to perform these conversions, along with example codes.
Problem Scenarios
Scenario 1: Adding Tuple Elements to List
You are given a list and a tuple as input, your task is to add tuple elements into the list and print the list as output. For example:
# Input List and Tuple my_list = [3, 6, 9, 45, 66] my_tup = (11, 14, 21) # Desired Output [3, 6, 9, 45, 66, 11, 14, 21]
Scenario 2: Adding List Elements to Tuple
In this case, you are given the same list and tuple as input, now your task is to add the list elements into the tuple and print the tuple as output. For example:
# Input List and Tuple my_list = [3, 6, 9, 45, 66] my_tup = (11, 14, 21) # Desired Output (11, 14, 21, 3, 6, 9, 45, 66)
Adding Tuple to a List
To add tuple to a list in Python, we can use the '+' operator, extend() or append() functions. Let's see an example for each of these methods ?
Using + Operator
The '+' operator is used to concatenate two lists in Python. In this method, first we convert the tuple into a list using the list() function and then we add those lists using + operator ?
my_list = [3, 6, 9, 45, 66]
my_tup = (11, 14, 21)
# Convert tuple to list and concatenate
my_list = my_list + list(my_tup)
print("The list after addition is:")
print(my_list)
The list after addition is: [3, 6, 9, 45, 66, 11, 14, 21]
Using extend() Method
The extend() method is a built-in list method used to add all elements from an iterable (such as another list, tuple, string, or set) to the end of an existing list. Here, we can use this method to add tuple elements to a list ?
my_list = [3, 6, 9, 45, 66]
my_tup = (11, 14, 21)
# Extend list with tuple elements
my_list.extend(my_tup)
print("The list after addition is:")
print(my_list)
The list after addition is: [3, 6, 9, 45, 66, 11, 14, 21]
Using append() Method
Python's append() method is a built-in function used with lists to add a single element to the end. This method adds the entire tuple as a single element to a list ?
my_list = [3, 6, 9, 45, 66]
my_tup = (11, 14, 21)
# Append tuple as single element
my_list.append(my_tup)
print("The list after addition is:")
print(my_list)
The list after addition is: [3, 6, 9, 45, 66, (11, 14, 21)]
Adding List to a Tuple
Tuples are immutable, which means we cannot change or add elements to a tuple once it is created. So, to add elements of a list into a tuple, we create a new tuple that contains the original tuple and the list elements.
To add a list to a tuple, we convert the list object into a tuple object using tuple() function and then use the '+' operator to combine both tuples ?
my_tuple = (1, 2, 3)
my_list = [4, 5]
# Convert list to tuple and concatenate
new_tuple = my_tuple + tuple(my_list)
print("The new tuple after addition is:")
print(new_tuple)
The new tuple after addition is: (1, 2, 3, 4, 5)
Comparison
| Method | Modifies Original | Result Type | Best For |
|---|---|---|---|
| List + operator | No | New list | Creating new list |
| extend() | Yes | Modified list | Adding elements in-place |
| append() | Yes | List with tuple as element | Adding tuple as single item |
| Tuple + operator | No | New tuple | Creating new tuple |
Conclusion
Use extend() to add tuple elements to a list in-place, or + operator to create a new list. For adding list to tuple, convert the list to tuple and use + operator since tuples are immutable.
