Let’s say that you are going to have a party, and you have a list of friends that you are going to invite. This list may grow as you think of people you want to invite.
This is where the Python extend() method comes in. The extend() method allows you to add an element, or multiple elements, to the end of a list.
This article will teach you how to use the Python extend() method. We’ll use the invitation list example to illustrate our explanation.
What Is Python extend()?
The Python extend() method is one of Python’s list methods. The other list methods are Python append() and Python insert(). All of these methods allow you to manipulate lists in Python. Let’s dig into why you might want to use the Python .extend() method.
How Python .extend() works
The .extend() method extends a Python list by adding multiple items to the end of the list. Here are two ways you can format the syntax.
One way is to put the items you want added to the list (called “arguments”) within brackets inside the parentheses. Look at the second line in this example, where we have the new names as arguments within brackets inside parentheses:
invite_list = ['Shazia', 'Matt', 'Elizabeth'] invite_list.extend(['Janie','Bryce','Jaspar']) print(invite_list)
This code returns:
['Shazia', 'Matt', 'Elizabeth', 'Janie', 'Bryce', 'Jaspar']
Another way is to declare a new variable called new_names and then put this as the argument in the parentheses, as in the third line in this example:
invite_list = ['Shazia', 'Matt', 'Elizabeth'] new_names = ['Janie', 'Bryce', 'Jaspar'] invite_list.extend(new_names) print(invite_list)
This code returns the same result:
['Shazia', 'Matt', 'Elizabeth', 'Janie', 'Bryce', 'Jaspar']
When Not to Use Python extend()
If you only want to add one item to the end of your Python list, you can use the Python append() method instead of the extend() method. The Python append() method only adds a single item (such as an individual list item or another list) at a time.
For example, this code:
invite_list = ['Shazia', 'Matt', 'Elizabeth'] new_names = ['Janie', 'Bryce', 'Jaspar'] invite_list.append(new_names) print(invite_list)
results in this output:
['Shazia', 'Matt', 'Elizabeth', ['Janie', 'Bryce', 'Jaspar']]
Notice that the names that were added (Janie, Bryce, and Jaspar) are enclosed in their own set of brackets. This means that the Python append() method treated them as a single entity.
If you want to insert a list item in a specific position, rather than adding it to the end of the list, you should use the Python insert() method. In Python, list items are assigned positions. These positions are numbers, starting at zero. Therefore, the items in the list [‘Shazia’, ‘Matt’, ‘Elizabeth’] would be assigned 0, 1, 2, 3. ‘Shazia’ is at position 0, ‘Matt’ is at position 1, and so on.
To use the Python insert() method with our invitation list example, we can state the desired position, and the name we want to insert into that position, in the parentheses on the second line of this code:
invite_list = ['Shazia', 'Matt', 'Elizabeth'] invite_list.insert(0, 'Bryce') print(invite_list)
This code returns this result:
['Bryce', 'Shazia', 'Matt', 'Elizabeth']
We have inserted the name ‘Bryce’ into the 0 position in our list.
Conclusion
The Python extend() method is used to add items to the end of a list. To do this, you can simply put the new list items in the parentheses when you call the method. Alternatively, you can declare a new variable containing the new list items and then put the variable in the parentheses. The Python extend() method differs from the append() method, which only adds a single item at a time, and the insert() method, which puts a list item in a specific position in the list.
Now that you know how to use the Python extend() method, you can practice using it, along with the other Python list methods, the next time you code.
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.