Python List extend() Method
The Python List extend() method adds items of an iterable (list, tuple, dictionary, etc) at the end of a list.
Example
Python3
cars = [ "Audi" , "BMW" , "Jaguar" ] other_cars = [ "Maruti" , "Tata" ] cars.extend(other_cars) print (cars) |
Output
['Audi', 'BMW', 'Jaguar', 'Maruti', 'Tata']
List extend() Syntax
list_name.extend(iterable)
Parameters:
- iterable: Any iterable (list, set, tuple, etc.)
Returns
Python List extend() returns none.
What is Python List extend() method?
List extend() function is an in-built function of Python that extends a list by adding values of an iterable (list, tuple, string, etc) at the end of that list.
List extend() function is very useful when you don’t want to add values from an iterable individually. It saves a lot of time and effort when adding values from an iterable.
How to use list extend() method in Python
You can easily extend a list using extend() function. let’s see how to extend a list in Python with an example for better understanding.
Example: In this example, we can see how to extend a list in Python.
Python3
#creating list shopping = [ "apple" , "milk" , "cookies" ] more_items = [ "diaper" , "baby oil" ] #extending shopping list shopping.extend(more_items) #printing shopping list print (shopping) |
Output
['apple', 'milk', 'cookies', 'diaper', 'baby oil']
More Examples on List extend()
Let’s see some of the different scenarios where we use list extend() method.
1. Extend list with another List
The list.extend() method is equivalent to list[len(list):] = iterable. The list.extend() is Python’s built-in function that loops through the provided iterable, appending the elements to the end of the current list. Here we are extending a list with another list’s elements with extend() function.
Python3
l = [ 1 , 2 , 3 ] l.extend([ 4 , 5 , 6 ]) print (l) |
Output
[1, 2, 3, 4, 5, 6]
2. Extend List with Tuple
This code extends a list of an element with the element of a tuple with extend() in Python.
Python3
# My List my_list = [ 'Beginner' , 'for' , 'Beginner' ] # My Tuple my_tuple = ( 'DSA' , 'Java' ) # Append tuple to the list my_list.extend(my_tuple) print (my_list) |
Output
['Beginner', 'for', 'Beginner', 'DSA', 'Java']
3. Extend List with a Set
This code extends a list of an element with the element of set with extend() in Python.
Python3
# My List my_list = [ 'Beginner' , 'for' , 'Beginner' ] # My Set my_set = { 'Flutter' , 'Android' } # Append set to the list my_list.extend(my_set) print (my_list) |
Output
['Beginner', 'for', 'Beginner', 'Flutter', 'Android']
4. Extend List with a String
A string is iterable, so if you extend a list with a string, you’ll append each character as you iterate over the string.
Python3
# My list my_list = [ 'Beginner' , 'for' , 6 , 0 , 4 , 1 ] # My string my_list.extend( 'Beginner' ) print (my_list) |
Output
['Beginner', 'for', 6, 0, 4, 1, 'g', 'e', 'e', 'k', 's']
Python list Extend with + Operator
We can also extend a list without using the extend() function. You can see this with an example below.
In this code, We are extending the list with the + operator in Python.
Python3
fruits = [ 'apple' , 'banana' , 'cherry' ] more_fruits = [ 'orange' , 'grape' ] fruits + = more_fruits print (fruits) |
Output
['apple', 'banana', 'cherry', 'orange', 'grape']
Python list extend() vs append()
append() and extend() functions of lists add values at the end of the list, but they are very different. append() can only add one value at a time whereas extend() can add multiple values from an iterable to a list.
Example:
In the example, we have the list of fruits initially containing two elements: apple and banana. When we use the extend() method, the elements from more_fruits are appended individually to the list of the fruits, resulting in [‘apple’, ‘banana’, ‘cherry’, ‘orange’].
However, when we use the append() method, the more_fruits list itself is added as a single element at the end of the fruits list.
Python3
fruits = [ 'apple' , 'banana' ] more_fruits = [ 'cherry' , 'orange' ] fruits.extend(more_fruits) print (fruits) fruits = [ 'apple' , 'banana' ] fruits.append(more_fruits) print (fruits) |
Output
['apple', 'banana', 'cherry', 'orange']
['apple', 'banana', ['cherry', 'orange']]
We have discussed the definition, syntax and different uses of Python list extend() method. extend() method is very important operation on lists, and can save you a lot of time from manually extending a list.
Read Other Python List Methods
Similar Article on List extend:
Contact Us