The Python list object is the most general sequence provided by the language. Lists are positionally ordered collections of arbitrarily typed objects, and they have no fixed size. They are also mutable—unlike strings, lists can be modified in-place by assignment to offsets as well as a variety of list method calls.
Because they are sequences, lists support all the sequence operations we discussed for strings; the only difference is that results are usually lists instead of strings. For instance, given a three-item list:
>>> L = [123, 'spam', 1.23] # A list of three different-type objects
we can index, slice, and so on, just as for strings:
>>> L # Indexing by position
>>> L[:-1] # Slicing a list returns a new list
>>> L + [4, 5, 6] # Concatenation makes a new list too
>>> L # We're not changing the original list
Python’s lists are related to arrays in other languages, but they tend to be more powerful. For one thing, they have no fixed type constraint—the list we just looked at, for example, contains three objects of completely different types (an integer, a string, and a floating-point number). Further, lists have no fixed size. That is, they can grow and shrink on demand, in response to list-specific operations:
>>> L.append('NI') # Growing: add object at end of list
>>> L.pop(2) # Shrinking: delete an item in the middle
>>> L # "del L" deletes from a list too
Here, the listappend method expands the list’s size and inserts an item at the end; thepopmethod (or an equivalentdelstatement) then removes an item at a given offset, causing the list to shrink. Other list methods insert items at an arbitrary position (insert), remove a given item by value (remove), and so on. Because lists are mutable, most list methods also change the list object in-place, instead of creating a new one:
>>> M = ['bb', 'aa', 'cc']
The listsortmethod here, for example, orders the list in ascending fashion by default, andreversereverses it—in both cases, the methods modify the list directly.
Although lists have no fixed size, Python still doesn’t allow us to reference items that are not present. Indexing off the end of a list is always a mistake, but so is assigning off the end:
...error text omitted...
>>> L = 1
This is on purpose, as it’s usually an error to try to assign off the end of a list (and a particularly nasty one in the C language, which doesn’t do as much error checking as Python). Rather than silently growing the list in response, Python reports an error. To grow a list, we call list methods such asappendinstead.
One nice feature of Python’s core data types is that they support arbitrary nesting—we can nest them in any combination, and as deeply as we like (for example, we can have a list that contains a dictionary, which contains another list, and so on). One immediate application of this feature is to represent matrixes, or “multidimensional arrays” in Python. A list with nested lists will do the job for basic applications:
>>> M = [[1, 2, 3], # A 3 x 3 matrix, as nested lists
Here, we’ve coded a list that contains three other lists. The effect is to represent a 3× 3 matrix of numbers. Such a structure can be accessed in a variety of ways:
>>> M # Get row 2
>>> M # Get row 2, then get item 3 within the row
The first operation here fetches the entire second row, and the second grabs the third item within that row—stringing together index operations takes us deeper and deeper into our nested-object structure.*
Please check back next week for the continuation of this article.
blog comments powered by Disqus