A Wandering Mind · Computer Science
PYTHON GUIDE

Learn · Run · Break · Fix

A learning series from A Wandering Mind

Eighteen interlinked lessons on strings, lists, files, exceptions, and dictionaries—with original explanations, runnable examples, real output, and supporting CPSC 1301K lectures.

course.py
>>> guide = {
...   "lessons": 18,
...   "modules": 5,
...   "python": "3.10+"
... }
>>> guide["goal"] = "understand it"
Module 2 · Lists · Lesson 3 of 18

Creating Lists and Empty Lists in Python

Strings are excellent for one piece of text, but programs constantly need to keep several related values together. A Python list gives those values one ordered container. In this lesson, you will create lists with square brackets, build an empty list for values that arrive later, use the list() constructor, and learn why lists can hold more than one kind of value.

Prerequisite: Complete Splitting and Joining Strings first. That lesson already produced lists with split(); now we slow down and study the collection itself.

What a Python list actually represents

A list is one object that can refer to several values in a specific order. Instead of inventing a separate variable for every related item, you can place the items inside square brackets and assign the whole collection to one name. This becomes useful almost immediately: a shopping list, a series of temperatures, several usernames, steps in a process, or results collected while a program runs can all be represented as lists.

The square brackets are part of the list literal syntax. Items are separated by commas. Text values still need quotation marks because they are strings; numbers and Boolean values use their normal Python forms. The entire expression evaluates to a list object, and the variable on the left simply refers to that object.

Example 1 · A list literalVerified
languages = ["Python", "Java", "C"]
print(languages)
print(type(languages).__name__)
['Python', 'Java', 'C']
list

The output shows two useful things. First, Python prints the list with square brackets and represents each string with quotes. Second, the object’s type is list. The commas are separators between items; they are not part of the strings themselves.

A useful connection to the previous lesson: "red green blue".split() also returns a list. Lists are not limited to values you type directly between brackets. Other operations can create them for you.

Creating an empty list

An empty list is written as []. It is still a real list even though it currently contains no items. Empty lists matter because programs often do not know all of their data when execution begins. You may start with an empty collection and add values as a loop runs, a file is read, a user makes choices, or a calculation produces results.

This is a different idea from an “unknown” value. The list exists now; it simply has zero items. Because the container already exists, later code can keep using the same variable as the collection grows.

Example 2 · Start empty, then add valuesVerified
tasks = []
tasks.append("read")
tasks.append("practice")
tasks.append("build")
print(tasks)
['read', 'practice', 'build']

The append() calls add one item at a time to the end of the existing list. We will study list methods in much more detail later in the course; here, append() is only showing why an empty list is practical. The important design pattern is create the container first, then populate it as information becomes available.

Two common ways to create a list

Square-bracket syntax is the clearest choice when you already know the values you want. Python also provides the built-in list() constructor. The constructor is especially useful when you already have something iterable—something Python can step through one item at a time—and you want a list containing those items.

List literal["red", "green", "blue"]

Best when the values are already known and you want to write them directly.

Empty list literal[]

Best when the collection will be populated later.

Constructorlist("code")

Builds a list from another iterable value.

Empty constructorlist()

Also creates an empty list, although [] is shorter and very common.

Example 3 · Construct a list from a stringVerified
letters = list("code")
print(letters)
['c', 'o', 'd', 'e']

A string is iterable, so list("code") walks through the string character by character and places each character into the new list. Notice that this is not the same as writing ["code"]. The bracket version contains one string item; the constructor version above contains four one-character strings. That difference is a good reminder to ask what values you want the list to contain, not merely what syntax looks convenient.

Lists can hold different kinds of values

Python does not require every item in a list to have the same type. A single list can contain strings, integers, Boolean values, floating-point numbers, and even other objects. That flexibility is useful, although a list of similar things is often easier for humans to understand and for later code to process consistently.

Example 4 · Mixed value typesVerified
profile = ["Ada", 22, True, 3.9]
print(profile)
['Ada', 22, True, 3.9]

This is valid Python. Whether it is a good design depends on what the collection means. A short one-off record may be understandable, but if each position carries a different meaning, a dictionary can eventually become clearer because it gives those meanings names. We will reach dictionaries in Module 5. For now, remember the language rule: lists are allowed to mix types.

Silent mistake to watch for: adjacent string literals concatenate. Writing ["red" "green"] does not raise an error; Python produces ["redgreen"]. A missing comma can therefore change your data without producing a traceback. Commas matter.

Common mistakes when creating lists

The following failures were executed rather than invented. Read the final line of each traceback first: it names the exception and usually gives the fastest clue about what Python misunderstood.

Mistake 1 · Forgetting quotes around text
colors = [red, green]
print(colors)
Traceback (most recent call last):
  File "/tmp/awm_lesson3/mistake1_unquoted.py", line 1, in <module>
    colors = [red, green]
              ^^^
NameError: name 'red' is not defined

Python treats an unquoted word as a variable name. If the intended items are text, write ["red", "green"]. A NameError here is not really a list problem; it tells you Python tried to look up a name that was never defined.

Mistake 2 · Passing one integer to list()
numbers = list(42)
print(numbers)
Traceback (most recent call last):
  File "/tmp/awm_lesson3/mistake2_list_int.py", line 1, in <module>
    numbers = list(42)
TypeError: 'int' object is not iterable

The constructor expects something it can iterate through. A single integer is one value, not a sequence of smaller integer pieces. If you simply want a list containing the number 42, write [42].

Mistake 3 · Expecting an empty list to already contain an item
items = []
print(items[0])
Traceback (most recent call last):
  File "/tmp/awm_lesson3/mistake3_empty_index.py", line 2, in <module>
    print(items[0])
          ~~~~~^^^
IndexError: list index out of range

The empty list exists, but there is nothing inside it yet. [0] asks for the first position, and no such position exists. Lesson 4 will explain list indexes carefully; for this lesson, the important point is that “empty” really does mean zero elements.

Mistake 4 · Replacing Python’s list name with your own variable
list = ["already", "a", "list"]
letters = list("abc")
print(letters)
Traceback (most recent call last):
  File "/tmp/awm_lesson3/mistake4_shadow_list.py", line 2, in <module>
    letters = list("abc")
TypeError: 'list' object is not callable

list is the name of Python’s built-in constructor. Assigning your own list to that name hides the constructor in the current scope. Later, list("abc") tries to call your list object like a function. Prefer descriptive names such as items, tasks, or languages instead of reusing built-in names.

Supporting classroom explanations

This lesson is original A Wandering Mind instructional material and is meant to work without the videos. The two assigned CPSC 1301K lectures provide a concise classroom-style introduction to lists and to the specific idea of beginning with an empty list.

Lists Introduction — by Hyrum Carroll · 3:29. Watch on YouTube ↗ · CPSC 1301K playlist ↗

What it adds: a short lecture overview of list syntax and purpose, reinforcing the mental shift from one value to an ordered collection.

Empty Lists — by Hyrum Carroll · 2:44. Watch on YouTube ↗ · CPSC 1301K playlist ↗

What it adds: a focused explanation of why a program may deliberately create a list before it has any values to store.

Try it yourself

Type these rather than copying them if you can. The punctuation is part of the lesson: brackets create the list, commas separate items, and quotation marks distinguish literal text from variable names.

Exercise 1 · Create a three-item list

Create a list named snacks containing "apple", "pretzels", and "water". Print the list.

Show one answer
snacks = ["apple", "pretzels", "water"]
print(snacks)
['apple', 'pretzels', 'water']

Exercise 2 · Start with an empty collection

Create an empty list named packing. Append "charger" and "notebook", then print the result.

Show one answer
packing = []
packing.append("charger")
packing.append("notebook")
print(packing)
['charger', 'notebook']

Exercise 3 · Build a list from a string

Use list() with the string "Python". Predict how many separate string items will appear, then print the result.

Show one answer
letters = list("Python")
print(letters)
['P', 'y', 't', 'h', 'o', 'n']

What you should carry into Lesson 4

A list is an ordered collection object, and you can create one directly with square brackets, begin with an empty [], or construct one from an iterable with list(). You also know that the list can contain different Python types and that an empty list is still a valid container. The natural next questions are: how do you retrieve one item from that collection, and how do you determine how many items it contains?

Those questions lead directly to indexing and list length. Later lessons will build on the same list objects to change items, slice ranges, test membership, copy safely, call methods, loop through collections, and eventually compare lists with dictionaries.