A Step-by-Step Guide for Beginners

When you’re working with lists in python, there might be scenarios where you want to add multiple items at once. This guide will help you understand how to achieve that using append() method of list object and also showcase some other methods as well.

Updated October 5, 2023

When you’re working with lists in python, there might be scenarios where you want to add multiple items at once. This guide will help you understand how to achieve that using append() method of list object and also showcase some other methods as well.

We will use the following code snippet to demonstrate this:

# Start with an empty list
my_list = []
# Using the extend method, we can add multiple items at once
my_list.extend([1,2,3,4]) # This adds all these numbers to our list.
print(my_list)  # Output: [1, 2, 3, 4]

We can also use the + operator to combine lists:

# Start with an empty list
my_list = []
# Using '+' operator, we can add multiple items at once
my_list += [5,6,7,8] # This adds all these numbers to our list.
print(my_list)  # Output: [1, 2, 3, 4, 5, 6, 7, 8]

If you want to add a single item multiple times, then you can use the * operator before adding it to your list:

# Start with an empty list
my_list = []
# Using '*' operator and append method, we can add a single item multiple times.
my_list.append(9) 
my_list += my_list * 3 # This adds the number 9 to our list three times.
print(my_list)  # Output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 9, 9]

Here is another way using append() method:

# Start with an empty list
my_list = []
# Using append method and a tuple we can add multiple items at once.
my_list.append((10, 11)) # This adds the numbers 10 & 11 to our list.
print(my_list)  # Output: [(1, 2, 3, 4, 5, 6, 7, 8, 9, 9, 9), (10, 11)]

These are just a few ways you can add multiple items to your list in Python. Remember, the more you code, the better you’ll get! Happy coding!

Hey! Do you love Python? Want to learn more about it?
Let's connect on Twitter or LinkedIn. I talk about this stuff all the time!