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().
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
- Literals for known items.
- list() and split() for conversions.
- Comprehensions or append() to compute items.
Code examples
Literals and conversions
[10, 20, 30] [] ['a', 'b', 'c'] [2, 4, 6, 8, 10] ['red', 'green', 'blue']
Computed lists
[1, 4, 9, 16, 25] [0, 0, 0, 0]
More examples
Building lists from input and data
70 85 92
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:
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
Prefer
list() needs an iterable; an int is not iterable.
Nested repetition
Avoid
Prefer
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
- 1Create a list of the first ten multiples of 7 in two ways: with a loop and with a comprehension.
- 2Convert 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?