Modifying lists with methods
A list is a sequence of values that can contain any data type, unlike strings which contain only characters.
A List Can Change Shape
When a program needs to collect values, a list provides one sequence that can hold those values together. A list may begin empty and later contain elements as the program runs. Understanding what the list contains, which values count as its elements, and what variable refers to it gives you the foundation for modifying lists with methods.
List Structure and Syntax
A list is a sequence of values. Python creates a list with square brackets, placing elements inside and separating those elements with commas. The elements are the individual values held by the list. For example, in [4, 8, 15], the list has three elements: 4, 8, and 15.
An empty list is a valid list containing zero elements. It is written as [] and can be used as a starting point for building a list dynamically.
A string is a sequence containing only characters. A list is more flexible: its values do not have to be characters or even the same type. One list can contain integers, strings, floating-point values, and other types together.
Nested Values
A list can contain another list as one of its elements. The inner list is called a nested list. It remains a complete list, but it is stored as one element of the outer list. This lets a list organize data hierarchically.
In the source example ["spam", 2.0, 5, [10, 20]], the outer list has four elements. Those elements are the string "spam", the float 2.0, the integer 5, and the nested list [10, 20]. The values 10 and 20 belong to the nested list rather than being separate top-level elements of the outer list.
Names and List Data
Lists can be assigned to variables just as numbers and strings can. The variable name then refers to the entire list. Using the name gives the program a way to work with that list rather than repeatedly writing the complete square-bracket expression.
Starting with an Empty List
Represent a collection that begins with no elements and can receive values as a program runs.
- 1Create the list
Use [] to create a valid empty list with zero elements.
- 2Give it a name
Assign the list to a variable so the variable name refers to the entire list.
- 3Use it as a starting point
The empty list can later contain elements as the program runs.
An empty list is both a complete list object and a useful starting point for building a collection dynamically.
Reading a Modification
Before modifying a list, identify its current elements and their structure. For a list such as ["ready", 2, [5, 6]], the outer list contains three elements: a string, an integer, and a nested list. If values are added later, the list has a new collection of elements, but the nested list remains a list inside the outer list.
[]The list has zero elements.
["ready", 2, [5, 6]]The outer list has three elements: a string, an integer, and a nested list.
This before-and-after view emphasizes what to inspect when a list changes: count the outer elements, identify each value's type, and notice whether one of those elements is itself another list. The source material establishes that an empty list can later receive elements, but it does not specify named list methods or their individual syntax.
Mistakes to Avoid
Common mistake Treating a list as if it could contain only one data type
What it looks like[4, "four", 4.0]Why it failsLists can contain mixed data types, including integers, strings, floats, and other types.
Do this insteadInspect each element independently instead of assuming that every element has the same type.
Common mistake Forgetting that a nested list is one element of the outer list
What it looks like["spam", 2.0, 5, [10, 20]]Why it failsThe outer list has four elements. The values 10 and 20 belong inside the fourth element, which is the nested list.
Do this insteadSeparate the outer list's elements from the inner list's elements.
Common mistake Assuming [] means an invalid or missing list
What it looks like[]Why it failsAn empty list is a valid list with zero elements.
Do this insteadTreat [] as a useful starting point for a list that will receive elements later.
Common mistake Confusing a variable name with the list it refers to
What it looks likeA name such as scores refers to the entire list assigned to it.
Why it failsThe variable name provides a way to work with the list; it is not the collection of elements itself.
Do this insteadThink of the name as a reference to the complete list data.
Common mistake Assuming a list and a string have the same contents
What it looks likeA string contains characters, while a list can contain values of different types.
Why it failsThe two structures differ in the kinds of values they contain.
Do this insteadIdentify whether the value is a string of characters or a list of values.
Practice Check
Consider the list ["alpha", 7, ["beta", "gamma"]]. How many elements does the outer list contain? Which element is nested? What data types appear in the outer list?
Hints
- Count the comma-separated values at the outermost level.
- The bracketed expression inside the outer list is one element of that outer list.
- Look at the type of each outer element separately.
Practice Answer
Analyze ["alpha", 7, ["beta", "gamma"]].
- 1Count outer elements
There are three outer elements: "alpha", 7, and ["beta", "gamma"].
- 2Find the nested element
["beta", "gamma"] is the third outer element and is itself a list.
- 3Identify outer types
The outer list contains a string, an integer, and another list.
The outer list has three elements and contains mixed data types, including a nested list.
Key Takeaways
- 1A list is a sequence of values, while a string is a sequence of characters.
- 2Square brackets create lists, and [] creates a valid empty list with zero elements.
- 3Lists can contain mixed data types and can contain other lists as nested elements.
- 4A variable assigned a list refers to the entire list, giving the program a name through which it can work with that data.
- 5To understand a list modification, inspect the outer elements, the value types, and any nested list structure.
At a glance
- Lists are sequences that can contain values of any data type, unlike strings, which contain only characters.
- Square brackets create lists, including empty lists and lists containing nested lists.
- A list may contain mixed values such as strings, integers, floating-point values, and other lists.
- A variable name refers to the entire list assigned to it.
- An empty list can serve as a starting point for building a collection as a program runs.