Python 3 – List extend() Method
In Python 3, lists are used to store a collection of items. The items in a list can be of any data type, such as strings, integers, or even other lists. One of the methods available for manipulating lists is the .extend()
method. This method can be used to add multiple items to a list all at once, thereby making the list longer.
Syntax
The syntax for the .extend()
method is:
list1.extend(iterable)
Here, list1
is the name of the list that you want to extend, and iterable
is any iterable object (such as a list, tuple or string) containing the items that you want to add to the list.
Example
This example demonstrates the usage of the .extend()
method.
animals = ['cat', 'dog', 'horse']
new_animals = ['lion', 'tiger', 'monkey']
animals.extend(new_animals)
print(animals)
Output:
['cat', 'dog', 'horse', 'lion', 'tiger', 'monkey']
The above example adds the items in the new_animals
list to the end of the animals
list. After executing the .extend()
method, the animals
list now contains six elements: the original three animals, followed by the new animals that were added.
Appending Multiple Items
One common use for the .extend()
method is to append multiple items to a list at once, as opposed to appending them one at a time using the .append()
method.
For example, consider the following code:
numbers = [1, 2, 3]
numbers.append(4)
numbers.append(5)
numbers.append(6)
print(numbers)
Output:
[1, 2, 3, 4, 5, 6]
This code appends the numbers 4, 5, and 6 to the end of the numbers
list one at a time using the .append()
method.
The same result can be achieved more succinctly using the .extend()
method:
numbers = [1, 2, 3]
new_numbers = [4, 5, 6]
numbers.extend(new_numbers)
print(numbers)
Output:
[1, 2, 3, 4, 5, 6]
In this code, the .extend()
method is used to append the entire new_numbers
list to the end of the numbers
list all at once.
Adding a Single Item
It’s important to note that the .extend()
method is used to add multiple items to a list at once. If you want to add a single item to a list, you should use the .append()
method instead.
Consider the following code:
fruits = ['apple', 'banana', 'orange']
fruits.extend('grape')
print(fruits)
Output:
['apple', 'banana', 'orange', 'g', 'r', 'a', 'p', 'e']
In this code, the .extend()
method is used to add a single item to the fruits
list: the letter ‘g’. This happens because the string ‘grape’ is an iterable object, and the .extend()
method adds each item in the iterable object to the list separately.
If you want to add a single item to a list, you should use the .append()
method instead:
fruits = ['apple', 'banana', 'orange']
fruits.append('grape')
print(fruits)
Output:
['apple', 'banana', 'orange', 'grape']
In this code, the .append()
method is used to add the string ‘grape’ to the end of the fruits
list as a single item.
Conclusion
The .extend()
method is a useful tool for adding multiple items to a list at once. It can be used to simplify code that would otherwise require multiple .append()
calls. However, it’s important to note that the .extend()
method should only be used when you need to add multiple items to a list. If you want to add a single item to a list, you should use the .append()
method instead. With this knowledge, you can start using the .extend()
method to manipulate lists in your own Python programs.