Example 1: extend a list python
list1 = [1,2,3,4,5]
list1.extend([6,7,8])
print(list1)
Example 2: python extend
fruits = ['apple', 'banana', 'cherry']
cars = ['Ford', 'BMW', 'Volvo']
fruits.extend(cars)
print(fruits)
Example 3: what does .extend do in python
list = ['hello', 'bro']
my_list.append('append')
print (my_list)
>>> ['geeks', 'for', 'geeks']
my_list.append ([6, 0, 4, 1])
print (my_list)
>>> ['geeks', 'for', 'geeks', [6, 0, 4, 1]]
my_list = ['geeks', 'for']
another_list = [6, 0, 4, 1]
my_list.extend(another_list)
print (my_list)
>>> ['geeks', 'for', 6, 0, 4, 1]
my_list = ['geeks', 'for', 6, 0, 4, 1]
my_list.extend('geeks')
print (my_list)
>>>['geeks', 'for', 6, 0, 4, 1, 'g', 'e', 'e', 'k', 's']
Example 4: what is the use of extend in python
The extend() method adds the specified list elements (or any iterable) to the end of the current list.