How to merge two lists with Python extend

Python extend lets programmers add the elements of a second list to an already existing list. It’s important to note that the elements to append are added at the end of the list. To add an element at a specific index of a list or append only single elements, Python insert or Python append should be used.

The function and syntax of Python extend

The extend() method in Python operates in a straightforward manner. It takes a list and appends a second list to the end of it, effectively extending the first list with the elements of the second list. This process is commonly referred to as concatenation in programming. The syntax of extend() is simple:

list.extend(list2)
Python

In this example, the elements from list2 are appended to the end of list. However, this doesn’t return a new list with all elements. Instead, the original list is directly modified. The second list is not modified, unlike the original one. The functionality of extend() is illustrated in the following concrete example:

list = ["one", "two", "three"]
list2 = ["four", "five", "six"]
list.extend(liste2)
print(list) # output: ['one', 'two', 'three', 'four', 'five', 'six']
list.extend([7, 8, 9])
print(list) # output: ['one', 'two', 'three', 'four', 'five', 'six', 7, 8, 9]
Python

As you can see, the method behaves as expected. You can either create the list to be appended as an object in advance or pass it as a parameter when invoking the method. The example demonstrates that the elements in the lists being appended can have any data type. This is possible because a Python list can be heterogeneous, that is, it can contain elements of different data types.

Although extend() is most commonly used to concatenate two lists, you can use the method for other purposes. Beyond lists as parameters, Python extend accepts iterable objects (objects containing elements over which you can iterate) for input. These iterables include Python lists, Python strings, and Python dictionaries. When extend() is called, any iterable can be passed as a parameter. The components of the iterable are then added to the list one at a time. This feature is illustrated in the following example.

chars = ['a', 'b', 'c']
string = "def"
chars.extend(string)
print(chars) # Output: ['a', 'b', 'c', 'd', 'e', 'f']
Python

Here, a string of characters has been passed as an argument. The individual components of the string (the characters) have been added to the list one at a time.

Python extend alternatives

The extend method is an elegant solution to add elements of a list to another one. But there are some alternatives you need to know.

The addition operator

The addition operator (or plus sign) is one of the most common Python operators and predominantly used to add two numbers. However, it can be used for other purposes in Python, including merging lists. With the addition operator, it’s even possible to merge any number of lists at the same time. This is illustrated in the following example:

europe = ["Germany", "Finland"]
asia = ["China", "India"]
africa = ["Sudan", "Mali"]
countries = europe + asia + africa
print(europe) # output: ['Germany', 'Finland']
print(asia) # output: ['China', 'India']
print(africa) # output: ['Sudan', 'Mali']
print(countries) # output: ['Germany', 'Finland', 'China', 'India', 'Sudan', 'Mali']
Python

As observed, the alternative approach using the addition operator (+) returns a nearly identical result to the extend() method. However, there is a crucial difference between the two. Unlike extend(), the addition operator doesn’t modify the original lists; instead, it generates and returns a new list. Since a new list object is created in memory when using the addition operator, it can be relatively less efficient compared to extend() when merging two lists. This should be taken into account if you’re working with very large lists or if you want your program to be as efficient as possible.

Tip

Using the concatenation operator (+=) instead of the addition operator modifies the original list without creating a new list, thus avoiding the efficiency drawback. However, it’s important to note that extend() and += may not behave uniformly for all iterables.

The append method

The append method shares similar functionality with extend(), but it has a distinct characteristic in that it can only add a single element to a list. Consequently, it is not possible to pass an iterable as a parameter to this method. Let’s illustrate this with the following example:

list = [0, 1, 1, 2]
list.extend([3])
print(list) # output: [0, 1, 1, 2, 3]
list.append(5)
print(list) # output: [0, 1, 1, 2, 3, 5]
Python

Here, extend() and append() produce the same result, albeit with a slight difference. With append() you don’t need to specify the element to add within square brackets. However, if you intend to use append() to add multiple elements to the list, you would need to iterate over an existing list using a Python for loop. Hence, append() proves useful only when dealing with a single element and may not be suitable for adding multiple elements efficiently.

Tip

Want to publish your website or web application quickly and easily using Git? Then Deploy Now from IONOS is the perfect platform for you.

Was this article helpful?
Page top