Last modified: Sep 20, 2023 By Alexander Williams
Append to Empty List in Python [Examples]
Example 1: Using the `append()` Method
# Initialize an empty list
my_list = []
# Append items to the list
my_list.append("apple")
my_list.append("banana")
my_list.append("cherry")
# Print the updated list
print("My List:", my_list)
Output:
My List: ['apple', 'banana', 'cherry']
Example 2: Using List Comprehension
# Initialize an empty list and create a list of items to append
my_list = []
items_to_append = ["apple", "banana", "cherry"]
# Use list comprehension to append items
my_list.extend([item for item in items_to_append])
# Print the updated list
print("My List:", my_list)
Output:
My List: ['apple', 'banana', 'cherry']
Example 3: Using `+=` Operator with a List
# Initialize an empty list and create a list of items to append
my_list = []
items_to_append = ["apple", "banana", "cherry"]
# Use the `+=` operator to append items
my_list += items_to_append
# Print the updated list
print("My List:", my_list)
Output:
My List: ['apple', 'banana', 'cherry']
Example 4: Using `extend()` Method with an Iterable
# Initialize an empty list and create an iterable to append
my_list = []
items_to_append = ["apple", "banana", "cherry"]
# Use the `extend()` method to append items from an iterable
my_list.extend(items_to_append)
# Print the updated list
print("My List:", my_list)
Output:
My List: ['apple', 'banana', 'cherry']
Example 5: Using List Concatenation
# Initialize an empty list and create a list of items to append
my_list = []
items_to_append = ["apple", "banana", "cherry"]
# Use list concatenation to append items
my_list = my_list + items_to_append
# Print the updated list
print("My List:", my_list)
Output:
My List: ['apple', 'banana', 'cherry']