Parameters
The extend() method in Python accepts only one parameter, which is the iterable you want to add to the list. This parameter can be any iterable object such as a list, tuple, string, set, or even a dictionary. Here’s how it works with different data types:
- List: You can extend a list by adding elements from another list. The elements from the second list will be appended to the end of the first list.
- Tuple: A tuple can be extended into a list, with each element of the tuple added individually to the list.
- String: A string can be extended into a list of characters. Each character in the string is treated as a separate element and added to the list.
- Set: A set can be extended into a list, adding each element of the set to the list. Since sets are unordered collections, the order of the elements in the resulting list may not match the order in the set.
- Dictionary: When using a dictionary with extend(), only the keys of the dictionary will be added to the list. If you want to add key-value pairs, you would need to access the dictionary's values or items separately.
Returns
The extend() method does not return any value. Instead, it directly modifies the original list by adding the elements from the iterable. Since the operation modifies the list in place, the return value of the method is None. This behavior ensures that the original list is updated without needing to create a new list, making it memory-efficient.
Using extend() with Different Iterables
Let's now look at how the extend() method works with different types of iterables through examples.
Example 1: Using extend() with a List
In this example, we will extend a list with another list.
# Original list
my_list = [1, 2, 3]
# List to extend with
another_list = [4, 5, 6]
# Using extend() to add elements of another_list to my_list
my_list.extend(another_list)
# Output the updated list
print(my_list)

You can also try this code with Online Python Compiler
Run Code
Output:
[1, 2, 3, 4, 5, 6]
Explanation:
The extend() method adds all elements from another_list to my_list. After the operation, my_list contains all the elements from both lists.
Example 2: Using extend() with a Tuple
Now, let's extend a list with a tuple.
# Original list
my_list = ['a', 'b']
# Tuple to extend with
my_tuple = ('c', 'd')
# Using extend() to add elements of my_tuple to my_list
my_list.extend(my_tuple)
# Output the updated list
print(my_list)

You can also try this code with Online Python Compiler
Run Code
Output:
['a', 'b', 'c', 'd']
Explanation:
The extend() method adds each element of the tuple as an individual item to my_list. After the operation, my_list contains all the elements from both the list and the tuple.
Example 3: Using extend() with a String
In this example, we will extend a list with a string.
# Original list
my_list = ['apple', 'banana']
# String to extend with
my_string = "cherry"
# Using extend() to add characters of my_string to my_list
my_list.extend(my_string)
# Output the updated list
print(my_list)

You can also try this code with Online Python Compiler
Run Code
Output:
['apple', 'banana', 'c', 'h', 'e', 'r', 'r', 'y']
Explanation:
The extend() method treats each character of the string as an individual element. After the operation, each character of the string "cherry" is added as a separate element to my_list.
Example 4: Using extend() with a Set
Let's extend a list with a set now.
# Original list
my_list = [10, 20]
# Set to extend with
my_set = {30, 40}
# Using extend() to add elements of my_set to my_list
my_list.extend(my_set)
# Output the updated list
print(my_list)

You can also try this code with Online Python Compiler
Run Code
Output:
[10, 20, 30, 40]
Explanation:
The extend() method adds each element of the set to my_list. Sets are unordered, so the order of the elements may vary.
Frequently Asked Questionss
What is the difference between append() and extend()?
The append() method adds a single element to the end of a list. The extend() method adds each element from an iterable (like a list or tuple) to the end of the list.
Can extend() add a dictionary to a list?
Yes, extend() can add the keys of a dictionary to a list, but it will not add the values or key-value pairs as individual elements. It only adds the keys.
Will extend() change the original list?
Yes, the extend() method modifies the original list in place. It does not return a new list but changes the existing list directly
Conclusion
In this article, we discussed the Python extend() method, which allows you to add elements from various iterables to an existing list. We covered its syntax, parameters, return value, and demonstrated how it works with different types of iterables such as lists, tuples, strings, and sets. By using extend(), you can efficiently add multiple elements to a list in a single operation.
Recommended Readings: