|
||||
|
« Command Line Arguments, Part 1 | Command Line Arguments, Part 2 » |
Beginning Python Tutorial (Part 4)
by: crystalattice - Sep 03, 2005
Python ListsLists in Python are one of the most versatile collection object types available. The other two types are dictionaries 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:
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. MutabilityOne 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[1] = '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.
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[0] # 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.
|
GIDNetwork Sites
Archives
Recent GIDBlog Posts
Recent GIDForums Posts
Contact Us
|
« Command Line Arguments, Part 1 | Command Line Arguments, Part 2 » |