Python – Adding Tuple to List and Vice – Versa
Adding a tuple to a list in Python can involve appending the entire tuple as a single element or merging its elements into the list. Conversely, adding a list to a tuple requires converting the list to a tuple and concatenating it with the existing tuple, as tuples are immutable.
For example, adding a tuple (4, 5) to a list [1, 2, 3] can result in [1, 2, 3, (4, 5)] or [1, 2, 3, 4, 5], while combining a tuple (4, 5) with a list [6, 7] yields a new tuple (4, 5, 6, 7).
Using extend() and + operator
extend() adds each element of a tuple individually to the end of a list, making it useful for merging a tuple into an existing list element by element. The + operator concatenates two sequences e.g., tuples or lists to create a new one. When adding a list to a tuple, the list is first converted to a tuple using tuple() and then combined with the + operator to form a new tuple.
a = [1, 2, 3] # list
b = (4, 5) # tuple
c = [6, 7] # list to add
# add tuple to list `a`
a.extend(b)
print(a)
# add list to tuple `b`
d = b + tuple(c)
print(d)
Output
[1, 2, 3, 4, 5] (4, 5, 6, 7)
Explanation: extend() adds each element of b individually to a, resulting in [1, 2, 3, 4, 5]. To add c to b, c is converted to a tuple using tuple(c), and the + operator concatenates b and tuple(c) into a new tuple d.
Table of Content
Using append() and * operator
append() adds the entire tuple as a single element to the end of a list, making the tuple a nested item inside the list. The * operator expands the elements of sequences like tuples or lists. When adding a list to a tuple, * unpacks the elements of both tuple and list, combining them into a new tuple.
a = [1, 2, 3] # list
b = (4, 5) # tuple
c = [6, 7] # list to add
# add tuple to list `a`
a.append(b)
print(a)
# add list to tuple `b`
d = (*b, *c)
print(d)
Output
[1, 2, 3, (4, 5)] (4, 5, 6, 7)
Explanation: append() adds the entire tuple b as a single element to the list a, resulting in [1, 2, 3, (4, 5)]. To add c to b, the * unpacking operator is used to unpack the elements of both b and c. inside a new tuple d .
Using insert() and list comprehension
insert() places the entire tuple as a single element at a specified position in a list e.g., at the end using len(list) , resulting in a nested tuple within the list. List comprehension can be used to create a new sequence by iterating over elements and when adding a list to a tuple, it helps convert the tuple to another tuple before concatenating it with the list-turned-tuple.
a = [1, 2, 3] # list
b = (4, 5) # tuple
c = [6, 7] # list to add
# add tuple to list `a`
a.insert(len(a), b)
print(a)
# add list to tuple `b`
d = tuple(x for x in b) + tuple(c)
print(d)
Output
[1, 2, 3, (4, 5)] (4, 5, 6, 7)
Explanation: insert() inserts the tuple b as a single element at the end of the list a, resulting in [1, 2, 3, (4, 5)]. Using list comprehension, b is converted to a tuple and concatenated with tuple(c) using the + operator, forming a new tuple d .
Using list() and tuple()
list() converts a tuple into a list, allowing modifications such as adding new elements. The tuple() converts a list back into a tuple after changes. When adding a tuple to a list, list() helps treat the tuple as a sequence of individual elements, while adding a list to a tuple requires converting both to lists, combining and converting back using tuple().
a = [1, 2, 3] # list
b = (4, 5) # tuple
c = [6, 7] # list to add
# add tuple to list `a`
a.extend(list(b))
print(a)
# add list to tuple `b`
temp_list = list(b)
temp_list.extend(c)
d = tuple(temp_list)
print(d)
Output
[1, 2, 3, 4, 5] (4, 5, 6, 7)
Explanation: extend() converts b into a list and adds each element individually to the end of a, resulting in [1, 2, 3, 4, 5]. To add c to b, b is converted into a list, extended with c and converted back into a tuple d .