How to Create a List in Python | The School of Code

Settings

Appearance

Choose a typography theme that suits your style

Back to How-to Guides
Python

How to Create a List in Python

Learn different ways to create and initialize lists in Python, including list comprehensions.

PythonListsData Structures

Lists are one of Python’s most versatile data structures. Here’s how to create them.

Method 1: Square Bracket Notation

The most common way to create a list:

# Empty list
my_list = []

# List with items
fruits = ["apple", "banana", "cherry"]
numbers = [1, 2, 3, 4, 5]
mixed = [1, "hello", 3.14, True]

print(fruits)  # ['apple', 'banana', 'cherry']

Method 2: Using list() Constructor

Convert other iterables to lists:

# From a string
chars = list("hello")
print(chars)  # ['h', 'e', 'l', 'l', 'o']

# From a tuple
my_list = list((1, 2, 3))
print(my_list)  # [1, 2, 3]

# From a range
numbers = list(range(5))
print(numbers)  # [0, 1, 2, 3, 4]

Method 3: List Comprehension

Create lists with a concise syntax:

# Squares of numbers 0-4
squares = [x**2 for x in range(5)]
print(squares)  # [0, 1, 4, 9, 16]

# Filter with condition
evens = [x for x in range(10) if x % 2 == 0]
print(evens)  # [0, 2, 4, 6, 8]

Method 4: Repetition

Create a list with repeated elements:

# List of zeros
zeros = [0] * 5
print(zeros)  # [0, 0, 0, 0, 0]

# Repeated pattern
pattern = [1, 2] * 3
print(pattern)  # [1, 2, 1, 2, 1, 2]

Adding Items to Lists

fruits = ["apple", "banana"]

# Add single item
fruits.append("cherry")

# Add multiple items
fruits.extend(["date", "elderberry"])

# Insert at specific position
fruits.insert(1, "blueberry")

print(fruits)

Summary

  • Use [] for quick list creation
  • Use list() to convert other iterables
  • Use list comprehensions for creating lists from patterns
  • Use * operator for repetition