Skip to content

Module 8 · Python Data Structures — topic 3 of 22

List creation

All the ways to make a list: literals, list() from other iterables, range(), split(), repetition, comprehensions and building a list in a loop with append().

Beginner 10 min read·Lesson 106 of 306 in Python Programming

Learning objectives

  • Create lists from literals and other iterables
  • Build lists in loops and with comprehensions
  • Avoid the shared-inner-list trap

Definition

All the ways to make a list: literals, list() from other iterables, range(), split(), repetition, comprehensions and building a list in a loop with append().

Introduction

Lists can be written out directly, converted from other collections or text, or built up step by step. Knowing these options lets you create exactly the list you need in a single clear statement.

Why it matters

Data rarely arrives already in a list. It comes as text to split, ranges of numbers, results of a calculation per item, or input collected over time. Each situation has a natural way to create the list.

Detailed explanation

Literal: write the items directly, [10, 20, 30], or [] for an empty list.

list(iterable) converts any iterable: list("abc") gives ["a", "b", "c"], list(range(5)) gives [0, 1, 2, 3, 4], list(some_set) gives the set’s items.

str.split() creates a list of strings from text.

Repetition creates a list of identical items: [0] * 5. Only use this with immutable items; with lists inside, all rows would be the same object.

A comprehension builds a list from an expression: [n * n for n in range(1, 6)]. Module 10 covers comprehensions fully.

The accumulator pattern starts with an empty list and appends inside a loop, which is useful when each step needs several statements or input from the user.

Syntax

Python
numbers = [1, 2, 3]
letters = list("abc")
words = sentence.split()
squares = [n * n for n in range(1, 6)]
results = []
for item in items:
results.append(process(item))
  • Literals for known items.
  • list() and split() for conversions.
  • Comprehensions or append() to compute items.

Code examples

Literals and conversions

Python
print([10, 20, 30], [])
print(list("abc"))
print(list(range(2, 11, 2)))
print("red green blue".split())
Output
[10, 20, 30] []
['a', 'b', 'c']
[2, 4, 6, 8, 10]
['red', 'green', 'blue']

Computed lists

Python
squares = [n * n for n in range(1, 6)]
print(squares)
zeros = [0] * 4
print(zeros)
Output
[1, 4, 9, 16, 25]
[0, 0, 0, 0]

More examples

Building lists from input and data

Python
scores = []
for _ in range(3):
scores.append(int(input("Score: ")))
print("Scores:", scores)
line = "82, 67 ,91"
marks = [int(part) for part in line.split(",")]
print("Marks:", marks)
bad = [[0] * 2] * 2
good = [[0] * 2 for _ in range(2)]
bad[0][0] = good[0][0] = 5
print(bad, good)
Input
70
85
92
Output
Score: Score: Score: Scores: [70, 85, 92]
Marks: [82, 67, 91]
[[5, 0], [5, 0]] [[5, 0], [0, 0]]

Step-by-step explanation

  • The loop appends three converted inputs to an initially empty list.
  • split(",") gives three strings; int() tolerates their surrounding spaces.
  • [[0] * 2] * 2 repeats one inner list, so changing it changes both rows.
  • The comprehension creates a new inner list for each row.

Expected output

Running “Building lists from input and data” with the sample input shown prints exactly:

TextInput
70
85
92
TextOutput
Score: Score: Score: Scores: [70, 85, 92]
Marks: [82, 67, 91]
[[5, 0], [5, 0]] [[5, 0], [0, 0]]

Practical applications

  • Survey tools append each response to a list as it arrives.
  • Report scripts convert comma-separated values into lists of numbers.
  • Games create boards as lists of lists with comprehensions.

Best practices

  • Use literals for fixed data and list() for conversions.
  • Use comprehensions for simple transformations.
  • Never build nested lists with *.

Common mistakes

list() on a single number

Avoid

list(5)

Prefer

[5] # or list(range(5))

list() needs an iterable; an int is not iterable.

Nested repetition

Avoid

grid = [[" "] * 3] * 3

Prefer

grid = [[" "] * 3 for _ in range(3)]

Repetition copies references to the same inner list.

Key takeaways

  • Create lists with literals, list(), split(), * and comprehensions.
  • Build lists step by step with append().
  • Use comprehensions, not *, for lists of lists.

Try it yourself

  1. 1
    Create a list of the first ten multiples of 7 in two ways: with a loop and with a comprehension.
  2. 2
    Convert the string "4 8 15 16 23 42" into a list of integers.

Finished reading? Mark it complete to update your dashboard.

Suggested practice

13 questions cover the “Python Data Structures” module. Answers and explanations are revealed after you submit.

  • BeginnerWhat is printed?
  • BeginnerWhat is printed?
  • BeginnerWhat is printed?
Practise this module