Adding Tuples to Lists in Python

WhatsApp Image 2023 02 20 At 6 49 59 AM

Python is a very versatile language offering us various data structures. While programming in Python, we often work with lists and tuples. Both seem quite similar at first, but we can easily point out the differences with a closer look. According to what our program requires, we can make a fitting pick.

When working with lists, we might also come across a situation where we need to add a tuple to a list. There are two ways to execute it. We’ll be dealing with both ways in this article.

What are List and Tuple?

A list is a built-in data type in Python. It is a collection of ordered elements. It is used to store multiple items in a single variable, such as numbers or strings.

We should keep in mind that lists are mutable, meaning you can modify the elements in a list even after it has been created. You can also add or delete elements in a list. Lists are created by enclosing a sequence of elements in square brackets and separating them with commas. Here’s an example:

my_list= [1, 2, a, b]

In this example, my_list is a list containing four elements: 1, 2, a, and b.

A tuple is also a built-in data type in Python. It is a collection of ordered and unchangeable (immutable) elements.

A tuple is very similar to a list, but the key difference is that while lists are mutable by nature, tuples aren’t. Once created, we cannot modify, add or delete the elements in a tuple. Tuples are declared by enclosing a sequence of elements in round brackets separated with commas. Here’s an example:

my_tuple = (1, 2, a, b)

Now that we’ve looked into lists and tuples, let’s understand how to add a tuple to the end of a list.

Using the append() method

We can add a tuple to a list is by using the append() method. It is a built-in method of the list object. It is used to insert an element at the end of an existing list. When using append, an element gets added to the old list instead of a new list is returned. In this case, our element is a tuple.

The append() method adds a tuple to a list. It is a built-in method of the list object. It’s used to add an element to the end of an existing list. Rather than returning a new list when using append, an element is added to the existing list. Our element, in this particular case, is a tuple.

Let’s look at an example:

a = [1, 2, 3]
b = (4, 5, 6) 
a.append(b)
print(a)

Output of the above code:

[1, 2, 3, (4, 5, 6)]

In the above code, we have a list ‘a’ and a tuple ‘b’. We use the ‘append()’ to add ‘b’ to the end of ‘a’. We then print ‘a’.

Append Method
Append Method

Appending a tuple to a list using the concatenate operator

The concatenate operator can also be used to add a tuple to a list. We use the ‘+’ operator to combine two objects, such as two strings, two lists, or a list and a tuple.

The concatenate operator can be used to append a tuple to a list as shown below:

a = [1, 2, 3] 
b = (4, 5, 6) 
c = a + [b]
print(c)

Output:

[1, 2, 3, (4, 5, 6)]

In this example, we create a new list called ‘c’ by concatenating list ‘a’ with the tuple ‘b’. The resulting list contains the original elements of list ‘a’ followed by the tuple ‘b’.

Concatenate
Concatenate Method

Which Method to Use When Adding a Tuple?

While both the approaches demonstrated above to produce the same result, there are some differences between the two methods that we should be aware of.

Firstly, the original list is modified when using the append() method, whereas, with the concatenate operator, a new list is created. With append() method, since the original list is modified, any reference to the list will also see the change. On the other hand, with the concatenate operator, since a new list is created, the original list remains unchanged.

Secondly, the append() method is more efficient than the concatenate operator when you only need to add one or two elements to the list. The append() method modifies the list itself, meaning it does not need to create a new list. On the other hand, the concatenate operator needs to create a new list, which can be slower if the list is large.

Thirdly, the concatenate operator is more versatile than the append() method. The + operator can concatenate two objects of any type, including lists, tuples, and even strings, whereas the append() method can only add elements to the end of a list.

Versatility of the concatenate operator:

a = [1, 2, 3] 
b = (4, 5, 6) 
c = "Hello" 
new_list = a+[b]+[c]
print(new_list)

Output:

[1, 2, 3, (4, 5, 6), 'Hello']

In this example, we combine list ‘a’, tuple ‘b’, and string ‘c’ using the concatenate operator. The new list contains all the elements in the order they were concatenated.

Versatility Of Operator
Versatility Of Operator

We are now familiar with two methods for appending a tuple to a list. While the concatenate operator creates a new list and is considerably more versatile, the append() technique alters the original list and is more effective when adding only a few elements. We can choose which method to work better for our code based on our need.

References:

Official Documentation
Geeks For Geeks