Python by Swaroop C H - HTML preview

PLEASE NOTE: This is an HTML preview only and some elements such as links or page numbers may be incorrect.
Download the book in PDF, ePub, Kindle for a complete version.

Using Lists

Example 9.1. Using lists

#!/usr/bin/python
# Filename: using_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 end of the line for item in shoplist:
print item,

print '\nI also have to buy rice.'
shoplist.append('rice')
print 'My shopping list is now', shoplist

print 'I will sort my list now'
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 is now', shoplist

Output

$ python using_list.py
I have 4 items to purchase.
These items are: apple mango carrot banana
I also have to buy rice.
My shopping list is now ['apple', 'mango', 'carrot', 'banana', 'rice'] I will sort my list now
Sorted shopping list is ['apple', 'banana', 'carrot', 'mango', 'rice'] The first item I will buy is apple
I bought the apple
My shopping list is now ['banana', 'carrot', 'mango', 'rice']

How It Works

The variable shoplist is a shopping list for someone who is going to the market. Inshoplist, we only store strings of the names of the items to buy but remember you can add any kind of object to a list including numbers and even other lists.

We have also used thefor..in loop to iterate through the items of the list. By now, you must have realised that a list is also a sequence. The speciality of sequences will be discussed in a later section

Notice that we use a comma at the end of theprint statement to suppress the automatic printing of a line break after everyprint statement. This is a bit of an ugly way of doing it, but it is simple and gets the job done.

Next, we add an item to the list using the append method of the list object, as already discussed before. Then, we check that the item has been indeed added to the list by printing the contents of the list by simply passing the list to theprint statement which prints it in a neat manner for us.

Then, we sort the list by using the sort method of the list. Understand that this method affects the list itself and does not return a modified list - this is different from the way strings work. 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 by using thedel statement. Here, we mention which item of the list we want to remove and thedel statement removes it fromt he list for us. We specify that we want to remove the first item from the list and hence we usedel shoplist[0] (remember that Python starts counting from 0).

If you want to know all the methods defined by the list object, seehelp(list) for complete details.