To create a list in Python, put comma-separated values inside square brackets, as in fruits = ["apple", "banana", "cherry"]. A list is an ordered, changeable collection that can hold any mix of values, and an empty list is written []. You can also build lists with the list() constructor, a list comprehension, or by splitting a string.
This guide covers each way to create a list in Python 3.13 and 3.14, with the pitfalls beginners meet most often.
The most common way to create a list in Python is a list literal. Write the items between [ and ], separated by commas.
A list keeps the order you wrote, allows duplicates, and can contain values of different types. len() returns the number of items. A trailing comma after the last item is allowed and is handy in multi-line lists:
list() takes any iterable and builds a new list from its items. Called with no argument it returns an empty list.
Passing a string gives a list of characters, not a list with one string. To make a list with one string, write ["abc"]. The order of a list built from a set is not something to depend on, which is why the example sorts it before printing.
A list comprehension builds a list from a loop in one expression. It is the usual choice when each item is computed from another sequence.
The part after if filters items, and the expression before for transforms them. If the comprehension becomes hard to read, use a normal for loop with append() instead.
str.split() turns text into a list of words, and the * operator repeats a list.
Note that split(",") gives strings, so convert them with int if you need numbers: [int(x) for x in csv_row].
Repetition copies references, not objects. This is safe for numbers and strings but not for nested lists.
In the first version all three rows are the same list object. Use a comprehension to get independent rows.
Lists are mutable, so you can start empty and grow them.
| Method | What it does |
|---|---|
append(x) | adds one item at the end |
extend(iterable) | adds every item of an iterable at the end |
insert(i, x) | adds an item before index i |
Assigning a list to another name does not copy it. Both names point to the same list.
Use a.copy(), list(a) or a[:] for a shallow copy.
A list is the built-in, general-purpose sequence and can hold mixed types. The array module and NumPy arrays store values of one type compactly and suit numeric work.
Write items = [] or items = list(). Both give the same result, and the brackets are slightly faster and more common.
Yes. A list can hold any object, including other lists, which is how you build tables or grids. Create each inner list separately to avoid shared references.
Use list(range(1, 11)). The end value of range is excluded, so you pass 11.
Create a list with square brackets for known values, list() to convert another iterable, a comprehension to compute items, and split() to turn text into words. Remember that lists are mutable, repetition of nested lists shares references, and assignment does not copy.