There are several ways to add tuple to a list in Python.
Today, you will learn 6 ways to add tuple to a list in Python.
1: Add tuple to list using the append() method
The append() method adds an element to the end of a list.
To add a tuple to a list using append(), you can create the tuple and then append it to the list as follows:
my_list = [(1, 2), (3, 4)]
new_tuple = (5, 6)
my_list.append(new_tuple)
print(my_list)
Output
[(1, 2), (3, 4), (5, 6)]
We have a list of tuples named my_list.
Then we created a new tuple and named it new_tuple.
After that, we used the append() method to append the new_tuple to my_list.
2: Add a tuple to the list using the extend() method
The extend() method can be used to append a tuple to the end of a list.
To add a tuple to a list using extend(), you can create a tuple and then extend the list with it as follows:
my_list = [(1, 2), (3, 4)]
new_tuple = (5, 6)
my_list.extend([new_tuple])
print(my_list)
Output:
[(1, 2), (3, 4), (5, 6)]
One thing to notice here is that the extend() method accepts an iterable argument.
That’s why we passed new_tuple inside [ ].
3: Add tuple to list using the insert() method
The insert() method can be used to insert a tuple at a specific index in a list.
To add a tuple to a list using insert(), you can specify the index where you want to insert the tuple and then insert it as follows:
my_list = [(1, 2), (3, 4)]
new_tuple = (5, 6)
my_list.insert(1, new_tuple)
print(my_list)
Output
[(1, 2), (5, 6), (3, 4)]
On the third line, we used the insert method.
You noticed we passed two arguments to the insert() method.
Yes, the first one specifies the index.
This is the index where the tuple will be inserted.
And in the second argument, we passed the tuple.
4: Adding tuple to the list using the “+” operator
You can use the “+” operator to concatenate two lists.
To add a tuple to a list using “+”, you can create a new list containing only the tuple and then concatenate it with the original list as follows:
my_list = [(1, 2), (3, 4)]
new_tuple = (5, 6)
my_list = my_list + [new_tuple]
print(my_list)
Output
[(1, 2), (3, 4), (5, 6)]
We changed the new_tuple into a list by passing it into the [ ].
This converted the tuple into a new list.
After that, we concatenated the my_list with the newly created list.
5: Append tuple to list using append() method
You can use the list() function to convert a tuple to a list.
To add a tuple to a list using list(), you can convert the tuple to a list.
And then use any of the above methods to add it to the original list as follows:
my_list = [(1, 2), (3, 4)]
new_tuple = (5, 6)
new_list = list(new_tuple)
my_list.append(new_list)
print(my_list)
Output
[(1, 2), (3, 4), [5, 6]]
6: Insert a tuple to a specific index in a list
To insert a tuple at a specific index in a list, you can use the insert()
method.
The insert()
method takes two arguments:
- the index where you want to insert the tuple
- and the tuple itself
Here’s an example:
my_list = [1, 2, 3, 4, 5]
my_tuple = (6, 7, 8)
my_list.insert(2, my_tuple)
print(my_list)
Output
[1, 2, (6, 7, 8), 3, 4, 5]
We created a list my_list with five integers.
We also created a tuple my_tuple with three integers.
After that, we used the insert() method to insert the tuple at index 2 in my_list.
The resulting my_list contains the tuple at index 2.
And the other elements of my_list have been shifted to the right.
Discover more from Python Mania
Subscribe to get the latest posts sent to your email.