how to extend array in python 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: python append vs extend
my_list = [23, 11, 42, 24523]
# append will add it as if you're adding a new list to it
my_list.append([34523, 76979])
print(my_list)
# extend will go over each item in the new source list and add each
# element as part of the target list (my_list)
my_list.extend([12, 99])
print(my_list)
"""
Output:
[23, 11, 42, 24523, [34523, 76979]]
[23, 11, 42, 24523, [34523, 76979], 12, 99]
"""
Example 3: how to extend an array python
my_list = ['geeks', 'for']
another_list = [6, 0, 4, 1]
my_list.extend(another_list)