Example 1: python extend
fruits = ['apple', 'banana', 'cherry']
cars = ['Ford', 'BMW', 'Volvo']
fruits.extend(cars)
print(fruits)
Example 2: 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 3: extend in list python
subjects=["Maths","Science","Arts","Commerce"]
subjects_2=["Artificial intelligence","Statistics"]
subjects.extend(subjects_2)
print(subjects)
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.