list add code example

Example 1: python add to list

list_to_add.append(item_to_add)

Example 2: add to list python

list.append(item)

Example 3: python how to add to a list

food = "banana"
basket = []

basket.append(food)

Example 4: python add to list

list_to_add.append(item_to_add)
# or
list_to_add.push(item_to_add)

Example 5: python add to list

# Statically defined list
my_list = [2, 5, 6]
# Appending using slice assignment
my_list[len(my_list):] = [5]  # [2, 5, 6, 5]
# Appending using append()
my_list.append(9)  # [2, 5, 6, 5, 9]
# Appending using extend()
my_list.extend([-4])  # [2, 5, 6, 5, 9, -4]
# Appending using insert()
my_list.insert(len(my_list), 3)  # [2, 5, 6, 5, 9, -4, 3]

Example 6: add list python

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