How to Create a List in Python (with examples)

In this short guide, I’ll show you how to create a list in Python. I’ll also review the steps to access items in the list created.

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

ListName = ['Item1', 'Item2', 'Item3',....]

Let’s now see how to apply this template in practice.

How to Create a List in Python

Whenever you create a list in Python, you’ll need to make a distinction between:

  • Lists that contain strings, where each item within the list will be placed within quotes: ListName = [‘Item1’, ‘Item2’, ‘Item3’,….]
  • Lists that contain numeric values, where each item will not be placed within quotes: ListName = [Item1, Item2, Item3,….]
  • Lists that may contain a combination of strings and numeric values

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

(1) List of Names – this list will contain strings placed within quotes:

Names = ['Jon', 'Bill', 'Maria', 'Jenny', 'Jack']

(2) Age list – this list will contain numbers (i.e., integers) without quotes:

Age = [22,34,42,27,57]

Putting everything together, this is how the Python code would look to create the two lists:

Names = ['Jon', 'Bill', 'Maria', 'Jenny', 'Jack']
Age = [22,34,42,27,57]

print (Age)

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

How to Create a List in Python (Examples Included)

You can quickly verify that you created a list by adding the type() syntax as follows:

Names = ['Jon', 'Bill', 'Maria', 'Jenny', 'Jack']
Age = [22,34,42,27,57]

print (type(Age))

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

list type Python

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.

What do I mean by saying an “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:

ListName = ['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 this syntax:

ListName[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 ‘Names’ and ‘Age’ 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:

Names = ['Jon', 'Bill', 'Maria', 'Jenny', 'Jack']
Age = [22,34,42,27,57]

print (Age[2])

Run the code, and you’ll get the value of Maria from the ‘Names’ list, as well as the value of 42 from the ‘Age’ list (those are indeed the third values in each of those lists):

Access item in a List in Python

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

Names = ['Jon', 'Bill', 'Maria', 'Jenny', 'Jack']
Age = [22,34,42,27,57]


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

How to Create a List in Python

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

Names = ['Jon', 'Bill', 'Maria', 'Jenny', 'Jack']
Age = [22,34,42,27,57]


So the value that you’ll get is 34-22 = 12.

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