Lists in Python are one of the most versatile collection object types available. The other two types aredictionaries and tuples, but they are really more like variations of lists.
Python lists do the work of most of the collection data structures found in other languages and since they are built-in, you don't have to worry about manually creating them. Lists can be used for any type of object, from numbers and strings to more lists. They are accessed just like strings (like slicing and concatenation) so they are simple to use and they're variable length, i.e. they grow and shrink automatically as they're used. In reality, Python lists are C arrays inside the Python interpreter and act just like an array of pointers.
Here's a list of common list operations:
- L1 =  An empty list
- L2 = [0, 1, 2, 3] Four items: indexes 0.3
- L3 = ['abc', ['def', 'ghi']] Nested sublists
- L2 [n], L3[n][j] L2[n:j], len(L2) Index, slice, length
- L1 + L2, L2 * 3 Concatenate, repeat
- for x in L2, 3 in L2 Iteration, membership
- L2.append(4), L2.sort(), L2.index(1), L2.reverse() Methods: grow, sort, search, reverse, etc.
- del L2[k], L2[n:j] =  Shrinking
- L2[n] = 1, L2[n:j] = [4,5,6] Index assignment, slice assignment
- range(4), xrange(0, 4) Make lists/tuples of integers
The biggest thing to remember is that lists are a series of objects written inside square brackets, separated by commas. Dictionaries and tuples will look the same except they have different types of brackets.
I won't go into the simple actions for lists since they work just like string operations. If you have questions, look at the String Tutorial; if you still have questions, please post them. Just remember that the resulting object will be a new list (surrounded by square brackets) and not a string, integers, etc.
One of the special things about lists is that they are mutable, i.e. they can be modified in place without creating a new object. The big concern with this is remembering that, if you do this, it can affect other references to it. However, this isn't usually a large problem so it's more like something to keep in mind if you get program errors.
Here's an example of changing a list using offset and slicing:
Generic Code Example:
>>> L = ['spam', 'Spam', 'SPAM!'] >>> L = 'eggs' # index assignment >>> L ['spam', 'eggs', 'SPAM!'] >>> L[0:2] = ['eat', 'more'] # slice assignment: delete+insert >>> L # replaces items 0, 1 ['eat', 'more', 'SPAM!']
Python lists also support methods, which are basically object-specific functions:
Generic Code Example:
>>> L.append('please') # append method call >>> L ['eat', 'more', 'SPAM!', 'please'] >>> L.sort() # sort list items ('S' < 'e') >>> L ['SPAM!', 'eat', 'more', 'please']
I'll talk about methods more in a future tutorial, but for the curious, a method works like a function in that you have the method name followed by arguments in parentheses. The big difference is that a method is qualified to a specific object with the period sign. In the above example, L is the object, .append is the method, and('please') is the argument.
The append method simply adds a single item to the end of a list; it's different from concatenation by expecting a single object and not a list. It also changes the list in-place and doesn't create a brand new list object.
Here's a note about append and sort from the book "Learning Python" from O'Reilly.
Here's another thing that seems to trip up new users: append
and sort change the associated list object in-place, but don't return the list as a result (technically, they both return a value called None, which we'll meet in a moment). If you say something like L = L.append(X), you won't get the modified value of L (in fact, you'll lose the reference to the list altogether); when you use attributes such as append and sort, objects are changed as a side effect, so there's no reason to reassign.
Because lists are mutable, you can also use the del statement to delete an item or section. Here's an example:
Generic Code Example:
>>> L ['SPAM!', 'eat', 'more', 'please'] >>> del L # delete one item >>> L ['eat', 'more', 'please'] >>> del L[1:] # delete an entire section >>> L # same as L[1:] =  ['eat']
Finally, there's obviously more operations and methods for lists which can be found in the Python documentation. And you need to remember that only mutable objects can be changed in-place; strings, tuples, and other objects will always have to create new objects if you change them.