Python extend lets pro­gram­mers 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 straight­for­ward manner. It takes a list and appends a second list to the end of it, ef­fec­tive­ly extending the first list with the elements of the second list. This process is commonly referred to as con­cate­na­tion in pro­gram­ming. 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 func­tion­al­i­ty of extend() is il­lus­trat­ed 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 demon­strates that the elements in the lists being appended can have any data type. This is possible because a Python list can be het­ero­ge­neous, that is, it can contain elements of different data types.

Although extend() is most used to con­cate­nate two lists, you can use the method for other purposes. Beyond lists as pa­ra­me­ters, Python extend accepts iterable objects (objects con­tain­ing elements over which you can iterate) for input. These iterables include Python lists, Python strings, and Python dic­tio­nar­ies. When extend() is called, any iterable can be passed as a parameter. The com­po­nents of the iterable are then added to the list one at a time. This feature is il­lus­trat­ed 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 char­ac­ters has been passed as an argument. The in­di­vid­ual com­po­nents of the string (the char­ac­ters) have been added to the list one at a time.

Python extend al­ter­na­tives

The extend method is an elegant solution to add elements of a list to another one. But there are some al­ter­na­tives you need to know.

The addition operator

The addition operator (or plus sign) is one of the most common Python operators and pre­dom­i­nant­ly 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 il­lus­trat­ed 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 al­ter­na­tive approach using the addition operator (+) returns a nearly identical result to the extend() method. However, there is a crucial dif­fer­ence 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 rel­a­tive­ly 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 con­cate­na­tion operator (+=) instead of the addition operator modifies the original list without creating a new list, thus avoiding the ef­fi­cien­cy 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 func­tion­al­i­ty with extend(), but it has a distinct char­ac­ter­is­tic in that it can only add a single element to a list. Con­se­quent­ly, it is not possible to pass an iterable as a parameter to this method. Let’s il­lus­trate 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 dif­fer­ence. 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 ef­fi­cient­ly.

Tip

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

Go to Main Menu