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.
Create the list: Use [] to create a valid empty list with zero elements.
Give it a name: Assign the list to a variable so the variable name refers to the entire list.
Use 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.
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
Treating a list as if it could contain only one data type
Lists can contain mixed data types, including integers, strings, floats, and other types.
Fix:
Inspect each element independently instead of assuming that every element has the same type.Forgetting that a nested list is one element of the outer list
The outer list has four elements. The values 10 and 20 belong inside the fourth element, which is the nested list.
Fix:
Separate the outer list's elements from the inner list's elements.Assuming [] means an invalid or missing list
An empty list is a valid list with zero elements.
Fix:
Treat [] as a useful starting point for a list that will receive elements later.Confusing a variable name with the list it refers to
The variable name provides a way to work with the list; it is not the collection of elements itself.
Fix:
Think of the name as a reference to the complete list data.Assuming a list and a string have the same contents
The two structures differ in the kinds of values they contain.
Fix:
Identify 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"]].
Count outer elements: There are three outer elements: "alpha", 7, and ["beta", "gamma"].
Find the nested element: ["beta", "gamma"] is the third outer element and is itself a list.
Identify 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
- A list is a sequence of values, while a string is a sequence of characters.
- Square brackets create lists, and [] creates a valid empty list with zero elements.
- Lists can contain mixed data types and can contain other lists as nested elements.
- A variable assigned a list refers to the entire list, giving the program a name through which it can work with that data.
- To understand a list modification, inspect the outer elements, the value types, and any nested list structure.
Key Takeaways
- 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.