Example:
tuple = ("python", "includehelp", 43, 54.23)
Adding a Tuple to a list
We have a list of elements and we will be adding a tuple to this list and then returning back a tuple consisting of all elements in a list.
Example:
Input:
myList = [3, 6, 1] , myTuple = (2, 9, 4)
Output:
[3, 6, 1, 2, 9, 4]
We can add a tuple to a list by taking the list and then adding the tuple value using += operator or list.extend() method to add the tuple at the end of our list.
Syntax:
- += Operator: obj1 += obj2
- extend() method: list_name.extend(collection)
Program to add a tuple to a list in Python
Output:
Adding a list to a Tuple
In a similar way as we saw above, we can add a list to a tuple by first converting the tuple to a list then adding the list to it. And then converting the resulting list back to tuple.
Program to add a list to tuple in Python
Output:
need an explanation for this answer? contact us directly to get an explanation for this answer