Skip to Content
Learn
Creating and Modifying a List in Python
Growing a List: Plus (+)

When we want to add multiple items to a list, we can use + to combine two lists.

Below, we have a list of items sold at a bakery called items_sold:

items_sold = ['cake', 'cookie', 'bread']

Suppose the bakery wants to start selling 'biscuit' and 'tart':

>>> items_sold_new = items_sold + ['biscuit', 'tart'] >>> print(items_sold_new) ['cake', 'cookie', 'bread', 'biscuit', 'tart']

In this example, we created a new variable, items_sold_new, which contained both the original items sold, and the new ones. We can inspect the original items_sold and see that it did not change:

>>> print(items_sold) ['cake', 'cookie', 'bread']

We can only use + with other lists. If we type in this code:

my_list = [1, 2, 3] my_list + 4

we will get the following error:

TypeError: can only concatenate list (not "int") to list

If we want to add a single element using +, we have to put it into a list with brackets ([]):

my_list + [4]

Instructions

1.

Jiho is still updating his list of orders. He just received orders for 'lilac' and 'iris'.

Use + to create a new list called new_orders that combines orders with the two new orders.

2.

Remove the # in front of the list broken_prices. If you run this code, you’ll get an error:

TypeError: can only concatenate list (not "int") to list

Fix the command by inserting brackets ([ and ]) so that it will run without errors.

Folder Icon

Sign up to start coding

Already have an account?