How to Create a List in Python (with examples)

In this short guide, you’ll see how to create a list in Python. You’ll also learn how to access items in the list created.

To start, here is a template that you may use to create a list in Python:

list_name = ['item1', 'item2', 'item3', ....]

You’ll now see how to apply this template in practice.

How to Create a List in Python

Let’s now review a simple example, where we’ll create two lists in Python:

(1) List of products – this list would contain strings (by placing the values within quotes):

products = ['microwave', 'oven', 'toaster', 'refrigerator', 'dishwasher']

(2) List of prices – this list would contain numbers (i.e., integers) without quotes:

prices = [300, 700, 120, 1300, 950]

Putting everything together, here is the Python code to create the two lists:

products = ['microwave', 'oven', 'toaster', 'refrigerator', 'dishwasher']
prices = [300, 700, 120, 1300, 950]

print(products)
print(prices)

Run the code in Python, and you’ll get the following two lists:

['microwave', 'oven', 'toaster', 'refrigerator', 'dishwasher']
[300, 700, 120, 1300, 950]

You can quickly verify that you created lists using type() as follows:

products = ['microwave', 'oven', 'toaster', 'refrigerator', 'dishwasher']
prices = [300, 700, 120, 1300, 950]

print(type(products))
print(type(prices))

You’ll now see that indeed you have two lists:

<class 'list'>
<class 'list'>

How to Access an Item within a list

You can access an item within a list in Python by referring to the item’s index.

Each item within a list has an index number associated with that item (starting from zero).

For example, recall that the template to create a list is:

list_name = ['item1', 'item2', 'item3', ....]

In that case, item1 has an index of 0, item2 has an index of 1, item3 has an index of 2 and so on.

How can you then access a specific item within a list?

To do that, you can use the following approach:

list_name[index of the item to be accessed]

In the context of our example, let’s say that you want to access item3 in both the ‘products’ and ‘prices’ lists.

Since the index of item3 is 2, you’ll therefore need apply the following Python code to print the third item in each of the lists:

products = ['microwave', 'oven', 'toaster', 'refrigerator', 'dishwasher']
prices = [300, 700, 120, 1300, 950]

print(products[2])
print(prices[2])

Run the code, and you’ll get the value of toaster from the ‘products’ list, as well as the value of 120 from the ‘prices’ list (those are indeed the third values in those lists):

toaster
120

You can also access a range of values in your lists. For instance, let’s say that you want to print the last 3 products in the ‘products’ list. You can then use the syntax below to accomplish this task, where the range of index values is 2:5:

products = ['microwave', 'oven', 'toaster', 'refrigerator', 'dishwasher']
prices = [300, 700, 120, 1300, 950]

print(products[2:5])

Here are the last 3 products that you’ll get:

['toaster', 'refrigerator', 'dishwasher']

You can even perform arithmetic operations. For example, if you want to deduct the first price (with an index of 0) from the second price (with an index of 1), you may then apply this code:

products = ['microwave', 'oven', 'toaster', 'refrigerator', 'dishwasher']
prices = [300, 700, 120, 1300, 950]

print(prices[1]-prices[0])

So the value that you’ll get is 700-300= 400:

400

You may also want to check the following tutorials about lists: