How to Add Tuples to Lists in Python?

Let us see how to add tuples to lists in Python in this tutorial. As a Python developer working on a project for one of my USA clients, I recently encountered a situation where I needed to combine data stored in tuples with an existing list. Let me show you how to achieve this task with examples and screenshots.

Add Tuples to Lists in Python

Lists are mutable, which means we can manipulate collections of related data. Ordered sequences of elements enclosed between square brackets []. They can contain elements of different data types and be modified after creation.

Tuples on the other hand, are immutable, which means once a tuple is created, its elements cannot be changed. Ordered sequences of elements are enclosed between parentheses (). Tuples are often used to store related pieces of data that should not be modified.

Read How to Convert Dictionary to List of Tuples in Python?

1. Use the Append() Method

The best way to add a tuple to a list in Python is by using the append() method. This built-in method inserts an element at the end of a list.

Here’s an example:

states = ['California', 'Texas', 'Florida']
new_state = ('New York',)
states.append(new_state)
print(states)

Output:

['California', 'Texas', 'Florida', ('New York',)]

I executed the above example code and added the screenshot below.

Add Tuples to Lists in Python

In this example, we have a list called states containing the names of three US states. We create a tuple new_state with a single-element 'New York'. By using the append() method, we add the new_state tuple to the end of the states list.

Check out How to Fix TypeError:‘tuple’ object is not callable in Python?

2. Use the Extend() Method

If you have multiple tuples that you want to add to a list, you can use the extend() method. This method takes an iterable (such as a list or tuple) and adds each element of the iterable to the end of the list.

Here’s an example:

cities = ['New York', 'Los Angeles', 'Chicago']
new_cities = (('Houston',), ('Phoenix',), ('Philadelphia',))
cities.extend(new_cities)
print(cities)

Output:

['New York', 'Los Angeles', 'Chicago', ('Houston',), ('Phoenix',), ('Philadelphia',)]

I executed the above example code and added the screenshot below.

How to Add Tuples to Lists in Python

In this case, we have a list cities containing the names of three US cities. We also have a tuple new_cities that contains three tuples, each with a single city name. By using the extend() method, we add each tuple new_cities to the end of the cities list.

Read How to Access Tuple Elements in Python?

3. Use the Zip Function

In some situations, you may have multiple lists that you want to combine into a list of tuples. The zip() function in Python allows you to achieve this easily.

Here’s an example:

first_names = ['John', 'Emma', 'Michael']
last_names = ['Doe', 'Smith', 'Johnson']
full_names = list(zip(first_names, last_names))
print(full_names)

Output:

[('John', 'Doe'), ('Emma', 'Smith'), ('Michael', 'Johnson')]

I executed the above example code and added the screenshot below.

Add Tuples to Lists in Python zip function

In this example, we have two lists: first_names and last_names, containing the first and last names of individuals. By using the zip() function and passing both lists as arguments, we create a new list full_names that contains tuples of the corresponding first and last names.

Check out How to Print a Tuple in Python?

4. Use the += Operator

Another way to add tuples to a list is by using the += operator. This operator is similar to the extend() method and allows you to concatenate a list with an iterable.

Here’s an example:

companies = ['Apple', 'Google', 'Microsoft']
new_companies = (('Amazon',), ('Facebook',))
companies += new_companies
print(companies)

Output:

['Apple', 'Google', 'Microsoft', ('Amazon',), ('Facebook',)]

In this example, we have a list companies containing the names of three US-based tech companies. We also have a tuple new_companies with two tuples, each containing the name of a company. By using the += operator, we concatenate the new_companies tuple with the companies list.

The += operator internally works similarly to the extend() method, adding each element of the iterable to the end of the list.

Read How to Check if a Tuple is Empty in Python?

Conclusion

In this tutorial, I explained how to add tuples to lists in Python. I covered four efficient methods to accomplish this task such as using append() method, extend() method, zip function, += operator.

You may like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.