8.2 More features of lists

The next example is just to show a lot of other stuff lists can do (for once I don't expect you to type it in, but you should probably play around with lists until you are comfortable with them.). Here goes:
demolist = ['life',42, 'the universe', 6,'and',7]
print 'demolist = ',demolist
demolist.append('everything')
print "after 'everything' was appended demolist is now:"
print demolist
print 'len(demolist) =', len(demolist)
print 'demolist.index(42) =',demolist.index(42)
print 'demolist[1] =', demolist[1]
#Next we will loop through the list
c = 0
while c < len(demolist):
    print 'demolist[',c,']=',demolist[c]
    c = c + 1
del demolist[2]
print "After 'the universe' was removed demolist is now:"
print demolist
if 'life' in demolist:
    print "'life' was found in demolist"
else:
    print "'life' was not found in demolist"
if 'amoeba' in demolist:
    print "'amoeba' was found in demolist"
if 'amoeba' not in demolist:
    print "'amoeba' was not found in demolist"
demolist.sort()
print 'The sorted demolist is ',demolist

The output is:

demolist =  ['life', 42, 'the universe', 6, 'and', 7]
after 'everything' was appended demolist is now:
['life', 42, 'the universe', 6, 'and', 7, 'everything']
len(demolist) = 7
demolist.index(42) = 1
demolist[1] = 42
demolist[ 0 ]= life
demolist[ 1 ]= 42
demolist[ 2 ]= the universe
demolist[ 3 ]= 6
demolist[ 4 ]= and
demolist[ 5 ]= 7
demolist[ 6 ]= everything
After 'the universe' was removed demolist is now:
['life', 42, 6, 'and', 7, 'everything']
'life' was found in demolist
'amoeba' was not found in demolist
The sorted demolist is  [6, 7, 42, 'and', 'everything', 'life']

This example uses a whole bunch of new functions. Notice that you can just print a whole list. Next the append function is used to add a new item to the end of the list. len returns how many items are in a list. The index function tell where the first location of an item is located in a list. Notice how demolist.index(42) returns 1 and when demolist[1] is run it returns 42. The line #Next we will loop through the list is a just a reminder to the programmer (also called a comment). Python will ignore any lines that start with a #. Next the lines:

c = 0
while c < len(demolist):
    print 'demolist[',c,']=',demolist[c]
    c = c + 1
Create a variable c which starts at 0 and is incremented until it reaches the last index of the list. Meanwhile the print statement prints out each element of the list.

The del command can be used to remove a given element in a list. The next few lines use the in operator to test if a element is in or is not in a list.

This next example uses these features in a more useful way:

menu_item = 0
list = []
while menu_item != 9:
        print "--------------------"
        print "1. Print the list"
        print "2. Add a name to the list"
        print "3. Remove a name from the list"
        print "4. Change an item in the list"
        print "9. Quit"
        menu_item = input("Pick an item from the menu: ")
        if menu_item == 1:
                current = 0
                if len(list) > 0:
                        while current < len(list):
                                print current,". ",list[current]
                                current = current + 1
                else:
                        print "List is empty"
        elif menu_item == 2:
                name = raw_input("Type in a name to add: ")
                list.append(name)
        elif menu_item == 3:
                del_name = raw_input("What name would you like to remove: ")
                if del_name in list:
                        item_number = list.index(del_name)
                        del list[item_number]
                else:
                        print del_name," was not found"
        elif menu_item == 4:
                old_name = raw_input("What name would you like to change: ")
                if old_name in list:
                        item_number = list.index(old_name)
                        new_name = raw_input("What is the new name: ")
                        list[item_number] = new_name
                else:
                        print old_name," was not found"
print "Goodbye"

And here is part of the output:

--------------------
1. Print the list
2. Add a name to the list
3. Remove a name from the list
4. Change an item in the list
9. Quit

Pick an item from the menu: 2
Type in a name to add: Jack

Pick an item from the menu: 2
Type in a name to add: Jill

Pick an item from the menu: 1
0 .  Jack
1 .  Jill

Pick an item from the menu: 3
What name would you like to remove: Jack

Pick an item from the menu: 4
What name would you like to change: Jill
What is the new name: Jill Peters

Pick an item from the menu: 1
0 .  Jill Peters

Pick an item from the menu: 9
Goodbye

That was a long program. Let's take a look at the source code. The line list = [] makes the variable list a list with no items (or elements). The next important line is while menu_item != 9:. This line starts a loop that allows the menu system for this program. The next few lines display a menu and decide which part of the program to run.

The section:

current = 0
if len(list) > 0:
        while current < len(list):
                print current,". ",list[current]
                current = current + 1
else:
        print "List is empty"
goes through the list and prints each name. len(list_name) tell how many items are in a list. If len returns 0 then the list is empty.

Then a few lines later the statement list.append(name) appears. It uses the append function to add a item to the end of the list. Jump down another two lines and notice this section of code:

item_number = list.index(del_name)
del list[item_number]
Here the index function is used to find the index value that will be used later to remove the item. del list[item_number] is used to remove a element of the list.

The next section

old_name = raw_input("What name would you like to change: ")
if old_name in list:
        item_number = list.index(old_name)
        new_name = raw_input("What is the new name: ")
        list[item_number] = new_name
else:
        print old_name," was not found"
uses index to find the item_number and then puts new_name where the old_name was.