xxxxxxxxxx
# there are different ways to append
lst = [1,2,3]
# 1) using append
lst.append(4)
# 2) using Extend
lst.extend([4,5,6,7])
xxxxxxxxxx
#append to list
lst = [1, 2, 3]
something = 4
lst.append(something)
#lst is now [1, 2, 3, 4]
xxxxxxxxxx
# list.append(x): add an item to the end
list_1 = ['A', 'B', 'C']
list_1.append('D') # -> ["A", "B", "C", "D"]
# list.insert(i, x): insert an item at a given position
list_2 = ['A', 'B', 'D']
list_2.insert(2, 'C') # -> ["A", "B", "C", "D"]
# list.extend(iterable): appending all items from an iterable
list_3 = ['A', 'B', 'C']
list_4 = ['D', 'E', 'F']
list_3.extend(list_4) # -> ['A', 'B', 'C', 'D', 'E', 'F']
xxxxxxxxxx
#append to list
lst = [1, 2, 3]
li = 4
lst.append(li)
#lst is now [1, 2, 3, 4]
.append("the add"): append the object to the end of the list.
.insert("the add"): inserts the object before the given index.
.extend("the add"): extends the list by appending elements from the iterable.
xxxxxxxxxx
letters = ["a", "b", "c", "d"]
# Add
letters.append("e") #Output: ['a', 'b', 'c', 'd', 'e']
letters.insert(0, "-") #Output ['-', 'a', 'b', 'c', 'd', 'e']
xxxxxxxxxx
list_of_names=["Bill", "John", "Susan", "Bob", "Emma","Katherine"]
new_name="James"
list_of_names.append(new_name)
# The list is now ["Bill", "John", "Susan", "Bob", "Emma","Katherine", "James"]
xxxxxxxxxx
# to add an item to a list
list.append(item)
# To extend an list with a new list
list1.extend(list2)