A list is a data structure that holds an ordered collection of items i.e. you can store a sequence of items in a list. This is easy to imagine if you can think of a shopping list where you have a list of items you want to buy, except that you probably have each item on a separate line in your shopping list whereas in Python you put commas in between them.
The list of items should be enclosed in square brackets so that Python understands that you are specifying a list. You can add, remove or search for items in a list.
Although, we have been generally delaying discussion of objects and classes till now, a little explanation is needed right now so that you can understand lists better. We will explore this topic in detail in its own chapter.
A list is an example of usage of objects and classes. When you use a variable i and assign an integer, say 5 to it, you can think of it as creating an object (instance) i of class (type) int. In fact, you can see help(int) to understand this better.
A class can also have methods i.e. functions defined for use with respect to that class only i.e. you can use these pieces of functionality only when you have an object of that class. For example, Python provides an append method for the list class which allows you to add an item to the list. For example, mylist.append('an item') will add that string to the list mylist. Note the use of dot notation for accessing methods of objects.
A class can also have fields which are nothing but variables defined for use with respect to that class only i.e. you can use these variables only when you have an object of that class. Fields are also accessed by dot notation, such as mylist.field.
Example 9.1. Using Lists
#!/usr/bin/python # Filename : list.py # This is my shopping list shoplist = ['apple', 'mango', 'carrot', 'banana'] print 'I have', len(shoplist), 'items to purchase.' print 'These items are:', # Notice the comma at the end for item in shoplist: print item, print '\nI also have to buy rice.' shoplist.append('rice') print 'My shopping list now is', shoplist shoplist.sort() print 'Sorted shopping list is', shoplist print 'The first item I will buy is', shoplist[0] olditem = shoplist[0] del shoplist[0] print 'I bought the', olditem print 'My shopping list now is', shoplist
$ python list.py I have 4 items to purchase. These items are: apple mango carrot banana I also have to buy rice. My shopping list now is ['apple', 'mango', 'carrot', 'banana', 'rice'] Sorted shopping list is ['apple', 'banana', 'carrot', 'mango', 'rice'] The first item I will buy is apple I bought the apple My shopping list now is ['banana', 'carrot', 'mango', 'rice']
The variable shoplist is a shopping list for someone who is going to the market. Here, I am storing just strings in the list but remember that you can add anything to the list i.e. you can add any object to the list - even numbers or other lists.
We have also used the for..in loop to go through the items of the list. By now, you should have realised that a list is also an example of a sequence. The speciality of sequences will be discussed in detail later.
Notice that we use a comma at the end of the print statement to suppress the automatic printing of a line break after every print statement. This is a bit of an ugly way of doing it, but it gets the job done.
Next, we add an item to the list using the append method of the list object, as discussed before. Then, we check that the item has been indeed added to the list by printing the contents of the list. Note that the print statement automatically prints the list in a neat manner for us.
Then, we sort the list by using the sort method of the list object. Remember that this method affects the list itself and does not return a changed list - this is different from strings. This is what we mean by saying that lists are mutable and that strings are immutable.
Next, when we finish buying an item in the market, we want to remove it from the list. We achieve this using the del statement. Here, we mention which item of the list we want to remove and the del statement removes it from the list for us. Then, we just print the list to check that it has been indeed removed from the list.
We can access members of the list by using their position as shown above. Remember that Python starts counting from 0. Therefore, if you want to access the first item in a list then you can use mylist[0] to get the first item in the list.
If you want to know all the methods defined by the list object, see help(list) for complete details.