Python List extend() method [With Examples]

In this Python tutorial, we will discuss the Python List extend() method. It is an essential method of the list class that allows us to add multiple elements to a list at once.

List extend() method in Python

Below are the topics that we are doing to discuss in this article:

  • Introduction to Python List extend() method
  • Syntax of the extend() method
  • Purpose and use cases of the extend() method

Python List extend() method

The extend() method is a built-in function in Python’s list class. It allows us to add multiple items to the end of a list, in a single call.

The method takes a single argument, which is an iterable object, such as a list, tuple, string, or any other Python object that supports iteration.

The syntax for the extend() method is as follows:

list_name.extend(iterable)

Here, list_name is the name of the Python list we want to extend, and iterable is the iterable object. The iterable object can contain any number of items, which will be added to the end of the Python list in the order they appear in the iterable.

extend() method in Python Lists Examples

Let’s dive into some examples to see the extend() method in action.

Add elements to Python list

The primary purpose of the extend() method is to add multiple elements to a Python list in one go. It can be beneficial when we want to add more than one element to a list without having to use the Python append() method multiple times.

For example:

# create a list with some items
us_currency_names = ['Dollar', 'Quarter', 'Dime']

# add items from a list using extend() method
more_currency_names = ['Nickel', 'Penny']
us_currency_names.extend(more_currency_names)

print(us_currency_names) 

In the above example, we first create a Python list us_currency_names with some US currency names.

We then create another Python list more_currency_names with some more currency names and use the extend() method to add all the elements from the list to the us_currency_names list.

Output:

Python List extend method
Python List extend method

Merge Python List with String

Merging a Python list with a string is another use case for the extend() method. However, it’s essential to be cautious when using strings, as they will be treated as a sequence of individual characters when passed to the extend() method.

To avoid this, wrap the string in a Python list or tuple before passing it to the extend() method.

For example:

us_landmarks = ["Statue of Liberty", "Grand Canyon", "Mount Rushmore"]
new_landmark = "Golden Gate Bridge"

us_landmarks.extend([new_landmark])
print(us_landmarks)

In this example, we have a Python list of US landmarks and want to add the "Golden Gate Bridge" string to it. To do so correctly, we wrap the string in a list and use the Python extend() method, ensuring the entire string is added as a single element to the landmarks list.

Output:

Python List extend method Example
Python List extend method Example

Merge Python List with Tuple

Merging a Python list with a tuple is a common use case for the extend() method. Here’s another example, where we use the extend() method to add items from a tuple:

# create a list with some items
us_states = ["California", "Texas", "Florida"]

# add items from a tuple using extend() method
us_states_tuple = ("New York", "Illinois", "Pennsylvania")
us_states.extend(us_states_tuple)

print(us_states)

In the above example, we first create a Python list us_states with some US state names. We then create a Python tuple us_states_tuple with some more state names and use the extend() method to add all the elements from the tuple to the list.

Output:

List extend method in Python
List extend method in Python

Merge Python List with Set

Merging a Python list with a set using the extend() method is a straightforward process.

In this example, we have a list of popular US sports and a set containing additional sports. We will use the Python extend() method to merge the list and the set, creating a single list containing all the sports.

us_sports_list = ["Basketball", "Baseball", "American Football"]
us_sports_set = {"Ice Hockey", "Soccer", "Golf"}

us_sports_list.extend(us_sports_set)
print(us_sports_list)

In this example, we have a Python list called us_sports_list containing popular US sports names as strings and a Python set called us_sports_set containing additional sports names as strings.

By using the extend() method, we merge the set elements into the list, resulting in a single list containing all the sports.

Output:

List extend method in Python Example
List extend method in Python Example

Note: The order of the elements from the set may not be maintained, as sets are unordered collections.

Conclusion

The Python List extend() method of the list class is a useful built-in function that allows us to add multiple elements to a list in one go. It is a simple, efficient, and versatile method that works with any iterable object.

You may also like to read the following articles: