Python List extend() Method
Overview
In Python, the list.extend() method is used to iterate over an iterable (string, tuple, list, set, or dictionary) and then add each element of the iterable to the end of the current list. The length of the list increases by the number of elements present in the iterable.
Syntax of extend() in Python
The syntax of the extend() method in Python is:
The list extend() method is written with the list to be extended and an iterable is passed to it.
Parameters of extend() in Python
The list extend() method has only one parameter, an iterable. The iterable can be a list, tuple (it is like an immutable list), string, set, or even a dictionary.
Return Values from extend()
The list extend() method doesn't return anything, it just modifies the given list.
Python List extend() Example
Code:
Output:
Explanation:
In the above example, we are adding the list swiss_watches to the end of the list watches using the list extend() method.
What is extend() in Python?
The list.extend() is Python's built-in function. It iterates over the specified iterable and appends its elements to the end of the current list. The list.extend() method is equivalent to list[len(list):] = iterable (appends the elements of the iterable after the list's last element).
How to Use the List extend() Method in Python?
We can use extend in Python by passing any iterable as an argument to it, be it a list, tuple, string, set, or dictionary.
Using the List extend() Method with a List
When we use the list extend() method with a list (my_list), all the elements of my_list gets added at the end of the current list.
Using the List extend() Method with a Tuple
When we use the list extend() method with a tuple (my_tuple), it behaves similarly to a list as all the elements of my_tuple gets added at the end of the current list too.
Using the List extend() Method with a String
When we use the list extend() method with a string (my_str), all the characters of my_str gets added at the end of the current list as different elements.
Using the List extend() Method with a Set
When we use the list extend() method with a set (my_set), it behaves similarly to a list as all the elements of my_list gets added at the end of the current list.
Using the List extend() Method with a Dictionary
When we use the list extend() method with a dictionary (my_dict), all the keys of my_dict gets added at the end of the current list as list elements. Instead of adding keys to the dictionary, we can also add its values at the end of the current list by passing the my_dict.values() to the list extend() method.
Differences between append() and extend() in Python
The list extend() method in Python is used to add the elements of the iterable at the end of the current list as list elements. The length of the iterable increases the length of the original list.
On the other hand, the list append() method is used to add the object at the end of the current list as a single element. The length of the original list is increased by 1.
This comparison below will help in better understanding of the difference between the two:
list.extend() | list.append() |
---|---|
# Extending a list using iterable | # Appending element to a list |
More Examples
Example 1: Adding elements to a List Using Another List
Code:
Output:
Explanation:
In the above example, we are adding the items of list y at the end of the list x using the list.extend() method.
Example 2: Adding Elements to a List Using a Tuple
Code:
Output:
In the above example, we add the items of tuple y at the end of the list x using the list.extend() method.
Example 3: Adding Elements to a List Using a String
Code:
Output:
Explanation:
In the above example, we are adding the characters of the string y at the end of the list x as different list elements using the list.extend() method.
Example 4: Adding Elements to a List Using a Set
Code:
Output:
Explanation:
In the above example, we are adding the elements of the set y at the end of the list x using the list.extend() method.
Example 5: Adding Elements to a List Using a Dictionary's Keys
Code:
Output:
Explanation:
In the above example, we are adding the keys of the dictionary y at the end of the list x using the list.extend() method.
To add the values of a dictionary, we can use dictionary.values().
Example 6: Adding elements to a List Using a Dictionary's Values
Code:
Output:
Explanation:
In the above example, we are adding the values of the dictionary y.values() at the end of the list x using the list.extend() method.
Example 7: Extending a List Using the list-slicing Technique
Code:
Output:
Explanation:
In the above example, we are adding the items of list y at the end of the list x using the list slicing technique (the items of y get added from the len(x) index in the list x).
Conclusion
- The list.extend() method in Python is used to iterate over an iterable and add its elements at the end of the original list.
- In Python, the list.extend() method doesn't return any value, it just modifies the original list.
- The list.extend() method behaves similarly to this list slicing technique: list[len(list):] = iterable
- We can use any iterable method with the list.extend().
See Also
Refer to these topics below: