Definition Lists: A Python list is an ordered sequence of arbitrary Python objects. It is a mutable object by itself so, unlike Python sets, you can modify a Python list.
In this article, you’ll learn everything you need to know on how to create Python lists.
Table of Contents
Overview — Creating a List in Python
There are many ways of creating a list in Python. Let’s get a quick overview in the following table:
Code
Description
[]
Square bracket: Initializes an empty list with zero elements. You can add elements later.
[x1, x2, x3, … ]
List display: Initializes an empty list with elements x1, x2, x3, … For example, [1, 2, 3] creates a list with three integers 1, 2, and 3.
[expr1, expr2, ... ]
List display with expressions: Initializes a list with the result of the expressions expr1, expr2, … For example, [1+1, 2-1] creates the list [2, 1].
[expr for var in iter]
List comprehension: applies the expression expr to each element in an iterable.
list(iterable)
List constructor that takes an iterable as input and returns a new list.
[x1, x2, ...] * n
List multiplication creates a list of n concatenations of the list object. For example [1, 2] * 2 == [1, 2, 1, 2].
You can play with some examples in our interactive Python shell:
n = 5
lst = [None] * n
print(lst)
# [None, None, None, None, None]
You can modify the element n as you like. For a detailed discussion on this topic, please visit my blog tutorial on How to Create a List with a Specific Size n? The tutorial also contains many visualizations to teach you the ins and outs of this method (and gives you some powerful alternatives).
Python Create List of Strings
You can use the previous method to create a list of n strings:
n = 3
lst = ['xyz'] * n
print(lst)
# ['xyz', 'xyz', 'xyz']
But do you want to create a list of numbers?
Python Create List of Numbers
Simply replace the default strings with the default numbers:
n = 3
lst = [1] * n
print(lst)
# [1, 1, 1]
A special case is the number:
Python Create List of Zeros
This creates a list of zeros:
n = 3
lst = [0] * n
print(lst)
# [0, 0, 0]
But what if you want to create a list of consecutive numbers 0, 1, 2, …?
Python Create List from Range
To create a list of consecutive numbers from x to y, use the range(x, y) built-in Python function. This only returns a range object which is an iterable. But you can convert the iterable to a list using the list(...) constructor:
You can see that the second argument (the stop index) is not included in the range sequence. The third argument of the range(start, stop, step) function is the optional step size that allows you to skip step numbers of the series of continuous numbers.
Python Create List from 0 to 100
A special situation arises if you want to create a list from 0 to 100 (included). In this case, you simply use the list(range(0, 101)) function call. As stop argument, you use the number 101 because it’s excluded from the final series.
You first initialize the list with empty tuples. Tuples are immutable so you cannot change them. But you can overwrite each list element with a new tuple like you did in the example with the third element and tuple (1, 2, 3).
Python Create Equally Spaced List
To create an equally spaced list, use the np.linspace() function of the NumPy library. Here’s a short tutorial on the matter: