Understanding Object Attributes
Lists are objects that bundle together data (the items they contain) and behavior (methods like append(), sort(), and __getitem__()).
A List Is More Than a Container
When you create a list, you are creating an object of type list. That object combines data, meaning the items it contains, with behavior, meaning the actions it can perform. The items are the list's stored data; methods such as append(), sort(), and __getitem__() are behaviors available through the list object.
What do you think happens?
Suppose a list receives the strings python, java, and c through append() calls and then sort() is called. What item will be retrieved by stuff[0]?
Reveal answer
c
append() adds each new item to the end. sort() then arranges the strings alphabetically, so c becomes the first item.
Following the List State
stuff = list()
stuff.append('python')
stuff.append('java')
stuff.append('c')
stuff.sort()
print(stuff)
print(stuff[0])- 1After stuff =
list(), the list contains no items. - 2After the first
append(), the list contains python. - 3After the next two
append()calls, the list contains python, java, and c in that insertion order. - 4After
sort(), the same list contains c, java, and python in alphabetical order. - 5The expression stuff[0] retrieves c because c is now at position 0.
- The
list()call constructs an empty list object. - Each
append()call adds one string to the end of the same list. sort()rearranges the existing strings alphabetically.- stuff[0] retrieves the item at position 0, which is the first item.
['c', 'java', 'python']
cThe important detail is that append() and sort() modify the list in place. The list first gains another item, and then its existing items are rearranged. These operations do not describe a separate result that replaces the list in this example; they change the list object you are already using. This ability to change after creation is why lists are described as mutable.
Methods as Object Behaviors
A method is a behavior or action that an object can perform. When you write stuff.append('python'), you invoke append() on the list object named stuff. When you write stuff.sort(), you invoke sort() on that same object.
The object-oriented perspective helps explain why list operations feel connected. A list is not only passive storage. It is an active object that knows how to add items to itself, sort its items, retrieve an item, and perform other list operations. The data and the actions that work with that data are bundled together.
Three Ways to Retrieve an Item
Python lets you express retrieval of the first item in a list in three equivalent ways: stuff[0], stuff.__getitem__(0), and list.__getitem__(stuff, 0). Each form retrieves the item at position 0. The square bracket form is the standard and most convenient syntax.
The double underscores in __getitem__ indicate a special method that Python uses internally when square bracket syntax is used. Calling stuff.__getitem__(0) makes the method call explicit on the list object. Calling list.__getitem__(stuff, 0) is even more explicit: it calls the method from the list class and passes stuff as the first parameter. Although these forms show different levels of detail, they retrieve the same item.
Use square bracket syntax for ordinary list item retrieval. The direct method forms are useful for understanding the relationship between Python syntax and object methods, but square brackets are the standard and most readable form.
Mistakes with List Objects
Common mistake Treating append() as if it only reports a change instead of changing the list.
What it looks likeCalling stuff.append('python') and continuing to reason as though stuff still has its old contents.
Why it failsappend()adds the item to the end of the list and modifies the list in place.Do this insteadUpdate your traced list state immediately after every append() call.
Common mistake Forgetting that sort() changes the order of existing items.
What it looks likeAssuming the first item remains the first item after stuff.sort().
Why it failssort()rearranges the items in order.Do this insteadWrite down the list before and after sort(), then use the new order when retrieving an item.
Common mistake Assuming the three retrieval forms select different items.
What it looks likeExpecting stuff[0] and stuff.__getitem__(0) to produce different results.
Why it failsThe three forms use different syntax but retrieve the same item at position 0.
Do this insteadFocus on the index and the list state; the syntax does not change which position is selected.
Common mistake Thinking of a list as only passive data storage.
What it looks likeDescribing stuff as only a container while ignoring append(), sort(), and __getitem__().
Why it failsA list object bundles its stored items with behaviors that act on those items.
Do this insteadWhen analyzing a list, identify both its current data and the methods available to operate on it.
Practice the State Trace
Tracing Retrieval After Sorting
A list receives the strings zebra, apple, and mango using append(), then sort() is called. Which item is retrieved by stuff[0]?
- 1Record insertion order
After the
append()calls, the list contains zebra, apple, and mango in the order they were added. - 2Apply sort()
sort()rearranges the strings alphabetically, producing apple, mango, and zebra. - 3Apply index 0
Index 0 selects the first item in the new order.
stuff[0] retrieves apple.
Create a state trace for a list that starts empty, receives the items 8, 3, and 5 through append() calls, and then receives a sort() call. Write the list after each change and identify the item returned by stuff[0].
Hints
- Record the list after each
append()call. sort()arranges numbers numerically.- Index 0 means the first item after sorting.
What to Remember
- 1A list is an object that bundles stored items with behaviors called methods.
- 2
append()adds an item to the end of a list, whilesort()rearranges the list's items; both modify the list in place. - 3stuff[0],
stuff.__getitem__(0), andlist.__getitem__(stuff, 0)retrieve the same item at position 0. - 4Square bracket syntax is the standard and most readable way to retrieve a list item.
- 5Understanding lists as objects prepares you to understand objects that combine data with the actions they can perform.
At a glance
- Lists combine data, in the form of stored items, with behavior, in the form of methods.
append()adds to the end of a list andsort()rearranges its items in place.- The expressions stuff[0],
stuff.__getitem__(0), andlist.__getitem__(stuff, 0)are equivalent ways to retrieve the item at position 0. - Tracing the list after every method call makes object state changes visible.
- Lists provide a concrete foundation for understanding object-oriented programming.