In Python, you can append an item to a list using the append() method. Here’s an example:
my_list = [1, 2, 3, 4] my_list.append(5) print(my_list) # Output: [1, 2, 3, 4, 5]
In this example, we’ve created a list called my_list with four items. We then use the append() method to add the number 5 to the end of the list. The updated list is then printed to the console.
You can also append multiple items to a list at once using the extend() method. Here’s an example:
my_list = [1, 2, 3, 4] my_list.extend([5, 6, 7]) print(my_list) # Output: [1, 2, 3, 4, 5, 6, 7]
In this example, we’ve used the extend() method to add three items to the end of the list. The items are passed as a list argument to the extend() method. The updated list is then printed to the console.