How to Create a List in Python

In this tutorial, you will learn how to create a list in Python.

TLDR solution

list_name = [element1, element2, element3, ...]

Step-by-Step Example

Create a List

Similarly to assigning a value to a variable (e.g., x = 'hello'), you create a list by putting one or more values between squared brackets. Let's create lists of different lengths and types (string, integers):

# empty list
fish_list0 = []

# length 1, string value
fish_list1 = ['pufferfish']

# length 2, mixed type (string, integer)
fish_list2 = ['salmon', 200]

# length 4, strings
fish_list3 = ['salmon', 'pufferfish', 'shark', 'mackerel']

Select an Element of a List

You select an element of a list by specfiying its index, which starts at 0. For example:

# select the second (index=1) element 
x = fish_list3[1]

Print the selection and you should receive the following output:

pufferfish

Select Multiple Elements of a List

You can also slice the list, i.e., select multiple consecutive elements:

# select all but the last element: ['salmon', 'pufferfish', 'shark']
fish_list3[:-1]

# select all but the first element: ['pufferfish', 'shark', 'mackerel']
fish_list3[1:]

# select elements 2 and 3: ['shark', 'mackerel']
fish_list3[1:2]

That's it! You just learned how to create and access a list in Python.