# How to Create a Python List?

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.

## 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:

You can play with some examples in our interactive Python shell:

Exercise: Use list comprehension to create a list of square numbers.

Let’s dive into some more specific ways to create various forms of lists in Python.

BELOW I’LL GIVE YOU A PDF CHEAT SHEET OF THE MOST IMPORTANT PYTHON LIST METHODS. So keep reading!

## Python Create List of Size

Problem: Given an integer `n`. How to initialize a list with `n` placeholder elements?

```# n=0 --> []
# n=1 --> [None]
# n=5 --> [None, None, None, None, None]```

Solution: Use the list concatenation operation `*`.

```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 =  * 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 =  * 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:

```print(list(range(2, 4)))
# [2, 3]

print(list(range(2, 6)))
# [2, 3, 4, 5]

print(list(range(2, 10, 2)))
# [2, 4, 6, 8]```

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.

```print(list(range(0, 101)))
# [0, 1, ..., 100]

print(list(range(101)))
# [0, 1, ..., 100]```

It’s actually not necessary to give the default start index 0, so you can just skip it.

## Python Create List with For Loop

To create a list with a for loop, you first initialize the empty list, and then subsequently append an element to the list:

```lst = []
for x in range(10):
# Append any initial element here:
lst.append(x)

print(lst)
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]```

## Python Create List of Lists

However, there’s a small problem if you want to create a list with mutable objects (such as a list of lists):

```lst = [[]] * n
print(lst)
# [[], [], [], [], []]

lst.append(42)

print(lst)
# [, , , , ]```

Changing one list element changes all list elements because all list elements refer to the same list object in memory:

The solution is to use list comprehension (see my detailed blog tutorial on list comprehension for a complete guide):

```lst = [[] for _ in range(n)]
print(lst)
# [[], [], [], [], []]

lst.append(42)
print(lst)
# [[], [], , [], []]```

In the following visualization, you can see how each element now refers to an independent list object in memory:

Exercise: Run the visualization and convince yourself that only one element is modified! Why is this the case?

## Python Create List of Tuples

A similar approach can be used to create a list of tuples instead of a list of lists.

```n = 5
lst = [() for _ in range(n)]
print(lst)

lst = (1, 2, 3)
print(lst)

'''
[(), (), (), (), ()]
[(), (), (1, 2, 3), (), ()]
'''```

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: