Learn
Creating and Modifying a List in Python
Growing a List: Append

We can add a single element to a list using .append(). For example, suppose we have an empty list called empty_list:

empty_list = []

We can add the element 1 using the following commands:

empty_list.append(1)

If we examine empty_list, we see that it now contains 1:

>>> print(empty_list) [1]

When we use .append() on a list that already has elements, our new element is added to the end of the list:

# Create a list my_list = [1, 2, 3] # Append a number my_list.append(5) print(my_list) # check the result

the output looks like:

[1, 2, 3, 5]

It’s important to remember that .append() comes after the list. This is different from functions like print, which come before.

Instructions

1.

Jiho works for a gardening store called Petal Power. He keeps a record of orders in a list called orders.

Use print to inspect the orders he has received today.

2.

Jiho just received a new order for 'tulips'. Use append to add this string to orders.

3.

Another order has come in! Use append to add 'roses' to orders.

4.

Use print to inspect the orders Jiho has received today.

Folder Icon

Sign up to start coding

Already have an account?