List Comprehensions and Advanced List Methods
Because lists are mutable, their contents can change after the list has been created. An append() call is one direct way to observe that mutability.
A List Has Data and Behavior
A list is more than a passive container. It is an object: a constructed value that bundles together data and behavior. Its data is the collection of items it contains. Its behavior is represented by methods such as append(), sort(), and __getitem__(). Thinking in these terms gives you a concrete introduction to the object-oriented idea that objects combine data with the actions they can perform.
Appending an Item
The append() method adds an item to the end of a list. The important point is that append() modifies the list object itself. After the call, the list contains one additional item; it is not merely associated with a separate result that leaves the original list unchanged.
Source example: when the list object named stuff receives the call stuff.append('python'), the item 'python' is added to the end of that list.
Because lists are mutable, their contents can change after the list has been created. An append() call is one direct way to observe that mutability.
Sorting the Same Object
The sort() method rearranges the items in a list. For strings, the source material describes this as alphabetical order; for numbers, it describes the order as numerical. Like append(), sort() modifies the list in place. The list remains the same object, but its contents now appear in a different order.
Following Two Mutations
Trace the list named stuff as it receives an appended item and is then sorted.
- 1Construct
The name stuff refers to a newly constructed list object containing its initial items.
- 2Append
Calling
append()adds one item to the end of that same list object. - 3Sort
Calling
sort()rearranges the items in that same object into the applicable order.
The final state is the same list object after two in-place changes: first its contents increase, then the order of its items changes.
Three Ways to Retrieve an Item
A list item can be retrieved using square brackets or by calling the special method behind that notation explicitly. The three equivalent forms described in the source are stuff[0], stuff.__getitem__(0), and list.__getitem__(stuff, 0). Each retrieves the item at position 0, which is the first item.
| Form | What it shows | Recommended use |
|---|---|---|
| stuff[0] | Square-bracket item retrieval | Standard and most convenient |
| stuff.__getitem__(0) | Direct call to the list object's special method | Useful for seeing what square brackets invoke |
| list.__getitem__(stuff, 0) | Call through the list class with the list passed as an argument | Most explicit and most verbose |
The three forms retrieve the same item at position 0, while differing in explicitness.
Use square-bracket syntax for ordinary item retrieval. The two __getitem__() forms are valuable because they reveal the method associated with the operation, but the source identifies square brackets as the standard and most convenient form.
Reading the Comprehension Topic
The supplied material names list comprehensions in the topic, but its detailed explanation focuses on lists as objects, list methods, in-place changes, and item retrieval. Those are the mechanisms you can establish confidently from this lesson’s source: a list stores items, methods act on the list, and retrieval expressions can expose an item at a position.
Mistakes Beginners Make
Common mistake Treating a list as only a container for data
What it looks likeThinking of the list as holding items but not having operations of its own
Why it failsThe source describes a list as an object that bundles data with behavior such as
append(),sort(), and__getitem__().Do this insteadWhen analyzing a list, identify both its current items and the methods that can act on it.
Common mistake Assuming append() leaves the original list unchanged
What it looks likeTracing append() as though it produces a separate list while the original remains the same
Why it failsappend()modifies the list in place by adding an item to its end.Do this insteadUpdate the state of the same list object after the method call.
Common mistake Forgetting that sort() changes order
What it looks likeContinuing to use the original order after sort() has been called
Why it failssort()rearranges the list items into the applicable alphabetical or numerical order.Do this insteadTrace both the items and their order after sort().
Common mistake Treating the three retrieval forms as different operations
What it looks likeAssuming stuff[0] retrieves a different item from stuff.__getitem__(0)
Why it failsThe source identifies all three forms as equivalent ways to retrieve the item at position 0.
Do this insteadRecognize square brackets as the standard notation for the same retrieval behavior.
Trace It Yourself
A list object named stuff is constructed. It then receives an append() call, a sort() call, and a retrieval using position 0. Describe the list’s state after each operation, then name the three equivalent retrieval expressions described in this lesson.
Hints
- After
append(), account for the new item at the end of the list. - After
sort(), account for the changed order. - Position 0 means the first item.
- The three forms are the square-bracket form, the object-method form, and the list-class form.
A reliable tracing habit is to write down the list’s state before a method call, apply the method’s documented behavior, and then write down the new state. For append(), inspect the added item. For sort(), inspect the new order. For retrieval, inspect the requested position.
Object-Oriented Perspective
Lists provide a familiar example of object-oriented programming. The list’s items are its data, while append(), sort(), and __getitem__() represent actions associated with that object. append() adds to the end, sort() rearranges in place, and the three retrieval forms access the same position. Once you can trace these changes on a list, you have a concrete model for the broader idea that objects combine data and behavior.
Key Takeaways
- A list is an object that combines stored items with methods that operate on those items.
append()adds an item to the end of the list and modifies the list in place.sort()rearranges the items in the same list object and also modifies it in place.- stuff[0],
stuff.__getitem__(0), andlist.__getitem__(stuff, 0)retrieve the same item at position 0. - Tracing a list means recording its state before and after each method call.