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']
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']
Contact Us