The extend()
method adds the specified iterable to the end of the given list.
expand()
iterates over an iterable, adding and extending the list with each element of the iterable.
list.extend(iterable)
The extend()
method requires an iterable (list, set, tuple, etc).
The following code shows how to use the extend()
method:
fruits = ['apple', 'banana', 'Avocado']numbers = (1, 6, 3)# Using extend() methodfruits.extend(numbers)print(fruits)
A string is iterable. If you extend a list with a string, each character of the string will be appended as you iterate over it.
# Creationg a listmy_list = ['Banana', 'Mango', 7,3]my_list.extend('Apple') #using extend()print(my_list)