Table of Contents
Add tuple to list in Python
Tuples and Lists are two of the most commonly used collection objects in Python. They both can store multiple elements and provide access to these elements using their indexes. There are a few differences associated with them as well.
Tuples are immutable, which means that once created they cannot be changed. Due to this, they require less memory and provide faster access to elements. Lists on the other hand are mutable and dynamic. They require more memory and access to elements is comparatively slow.
We will now discuss how to add tuple to list in Python.
Ways to Add Tuple to List in Python (adding the Tuple as An Element)
In this section of the article, we will discuss how to add a tuple as an element to the list.
Using the insert()
Function to Add Tuple to List in Python
The insert()
function can be used to add elements to a list at some specific position. We can provide the desired element along with the index in the function.
To add tuple to list in Python, we will provide the tuple as the element within the function.
See the code below.
1 2 3 4 5 6 |
lst = [(1,3),(7,8)] t = (5,6) lst.insert(1,t) print(lst) |
Output:
In the above example, we had a list of tuples, and we added a tuple to this list using the insert()
function.
Using the append()
Function to Add Tuple to List in Python
The append()
function is used to add elements towards the end of the list. We can specify the element within the function. In our example, we will add tuple to list in Python by specifying the tuple within the function.
For example,
1 2 3 4 5 6 |
lst = [(1,3),(7,8)] t = (5,6) lst.append(t) lst |
Output:
Further reading:
Ways to Add Tuple to List in Python (adding the Tuple’s Elements to A List)
In this section of the article, we will insert the elements of a tuple into a given list. Several methods are discussed below.
Using the extend()
Function to Add Tuple to List in Python
The extend()
function accepts an iterable and adds its elements to the end of a list. We can specify the tuple in the function to add tuple to list in Python.
1 2 3 4 5 6 |
lst = [1,3,7,8] t = (5,6) lst.extend(t) print(lst) |
Output:
In the above example, we add the elements from the tuple to the list using the extend()
function.
Using the +=
Operator to Add Tuple to List in Python
The += operator is termed as the concatenation operator that can combine elements from two iterables. We can add tuple to list in Python using this operator.
Remember that this operator only works for non-local objects (A variable that is not global nor local to a particular function).
See the code below.
1 2 3 4 5 6 |
lst = [1,3,7,8] t = (5,6) lst += t print(lst) |
Output:
Conclusion
To conclude, we discussed how to add tuple to list in Python. This was discussed in two sections.
In the first section, we discussed how to add a tuple as an element to the list. For this, we used the insert()
function that can add elements at a given index in a list and the append()
function that adds elements to the end of a list.
In the second section, we discussed how to add the tuple’s elements to a given list, essentially combining them both. The first method involved the use of the extend()
function and we used the concatenation operator (+=
) in the second method. Both perform the same function without much time difference.
That’s all about how to add tuple to list in Python.