python list.extend code example

Example 1: python array extend

my_list = ['a','b']  
my_list.append('c') 
print(my_list)      # ['a','b','c']

other_list = [1,2] 
my_list.append(other_list) 
print(my_list)      # ['a','b','c',[1,2]]

my_list.extend(other_list) 
print(my_list)      # ['a','b','c',[1,2],1,2]

Example 2: extend a list python

#adding two or more elements in a list
list1 = [1,2,3,4,5]
list1.extend([6,7,8])
print(list1)
#output = [1, 2, 3 ,4 ,5, 6, 7, 8]

Example 3: list.extend

# languages list
languages = ['French', 'English']

# another list of language
languages1 = ['Spanish', 'Portuguese']

# appending language1 elements to language
languages.extend(languages1)

print('Languages List:', languages)

Example 4: python extend

fruits = ['apple', 'banana', 'cherry']
cars = ['Ford', 'BMW', 'Volvo']

fruits.extend(cars)

print(fruits)
# ['apple', 'banana', 'cherry', 'Ford', 'BMW', 'Volvo']

Example 5: extend in list python

subjects=["Maths","Science","Arts","Commerce"]
subjects_2=["Artificial intelligence","Statistics"]
subjects.extend(subjects_2)
print(subjects)  ## used to add more elements from other list