Example 1: add something to list python
#append to list
lst = [1, 2, 3]
something = 4
lst.append(something)
#lst is now [1, 2, 3, 4]
Example 2: python add to list
list_to_add.append(item_to_add)
Example 3: append to list python
list = ["a"]
list.append("b")
print(list)
["a","b"]
Example 4: python how to append to a list
# Basic syntax:
your_list.append('element_to_append')
# Example usage:
your_list = ['a', 'b']
your_list.append('c')
print(your_list)
--> ['a', 'b', 'c']
# Note, .append() changes the list directly and doesn’t require an
# assignment operation. In fact, the following would produce an error:
your_list = your_list.append('c')
Example 5: python how to add to a list
food = "banana"
basket = []
basket.append(food)
Example 6: python how to add one list to another list
# Basic syntax:
first_list.append(second_list) # Append adds the the second_list as an
# element to the first_list
first_list.extend(second_list) # Extend combines the elements of the
# first_list and the second_list
# Note, both append and extend modify the first_list in place
# Example usage for append:
first_list = [1, 2, 3, 4, 5]
second_list = [6, 7, 8, 9]
first_list.append(second_list)
print(first_list)
--> [1, 2, 3, 4, 5, [6, 7, 8, 9]]
# Example usage for extend:
first_list = [1, 2, 3, 4, 5]
second_list = [6, 7, 8, 9]
first_list.extend(second_list)
print(first_list)
--> [1, 2, 3, 4, 5, 6, 7, 8, 9]