Lists and Tuples

Lists are a very useful part of programming. A list is essentially a variable that stores
more than one value and can be changed. A tuple is a list that is immutable, or cannot
be changed. Both of these can hold text or numbers or even other variables. We
declare a list like this:

mylist = [1, "hi", 2, "what's", 3, "up"]

or like this:

my list = 1, "hi", 2, "what's", 3, "up"

We can then output our list just like we would with any other variable!

print mylist

Now let’s say we only want to output one item in the list. How would we do this?
Well each item in the list is assigned a number, the first one being 0. So we could
output just one like this:

print mylist[1]

Which outputs the second variable in the list. We can also assign another variable to
an object in the list like so.

x = mylist[1] 
print x

Here is an example of a program that allows the user to choose an item in the list:

names = ["George", "Henry", "Theresa", "Janet", "Mark", "Justin",
"Michelle"]      
option = 0 #declare our input variable
while option != 99: #have the program run until user enters 99 
    option = input("Please enter a number between 0 and %d to quit enter
99:" %(len(names)-1))
    if 0 <= option < len(names):
        print names[option]   #print the name 
    elif option == 99:          #If option is 99 break out of if statement 
        quit
    else:
        print "That is not a valid option!"  #If option is anything else,
tell user

Now there are probably a couple different things in this one we haven’t covered. My
main focus is on the len() function. This is a function that returns the length of your
list. Used: len(list). You may also notice the %d on the input line. This is an
alternative way to get the value of a variable into a string. Usage: print “number is
%d” %23 (or %(24-1)). This would put the number 23 into the string. Now on that
same line, where I put len(names)-1, that is because the length of the list is alway
going to be 1 higher than the number assigned to the last variable. This is no problem
when you use a range()function though.

names = ["George", "Henry", "Theresa", "Janet", "Mark", "Justin",
"Michelle"]
for x in range(len(names)): 
print names[x]

This will simply output each name. Now what if we want to change an item in the list
to something else. This is very simple:

names[3] = "Bob"

And “Theresa” will be changed to “Bob” If we just want to add a name to the end of
the list we can use the append() function:

names.append("Georgina")

If we want to delete an item, we just use the remove() function.

names.remove("Henry")

Lets use the information we have so far to write an example program:

def menu():
print "\n\n\n1. List all employees "
print "2. Add employee"
print "3. Delete employee"
print "4. Number of employees"
print "0. Quit\n\n\n"
def list_employees(list): 
    for x in range(len(employees)): 
    print "%d." %(x+1), employees[x] 
def add_employee(list):
    addit = raw_input("Employee's name? ") 
    list.append(addit)
    return list 
def del_employee(list):
    delit = raw_input("What employee do you want to delete? ")
    if delit in list:
        list.remove(delit) 
        print delit, "deleted!"
    else:
        print "That employee is not in the database." 
    return list
option = 1 
employees = ["Jack Stark", "Fred Johnson", "Elise Smith"] 
x = 0 

while option != 0:
    menu()
    option = input("Please pick an option: ")
    print "\n\n\n"
    if option == 1: 
        list_employees(employees)
    elif option == 2:
        employees = add_employee(employees)
    elif option == 3: 
        employees = del_employee(employees)
    elif option == 4: 
        x = len(employees) 
        print "There are", x, "employees." 
    elif option == 0:
        quit 
    else:
        print "That is not a valid option"

This may seem like a sizable program, but once you read through it you should
understand just about everything in it. There is only one thing I haven’t covered and
that is in. In the line “if delit in list:”, the program just checks to see whether or not
that item is in the list. There are many more functions you can use with list, but I will
get to those as we need them. You should have enough information on lists to use for
multiple applications.
Now I mentioned tuples earlier. Basically they are the same as lists without all the
functionality. They are declared like this:

mytuple = (1, 2, 3, 4, 5)

You can still access all the objects in tuples, but you are unable to change them in
anyway. You can’t even add to them. Honestly, in most of my programs I use list as
they are more flexible.

Practice

  1. Make a counter that goes up to ten, using a list.
  2. Now add on to that so that the user can specify a number to count up to.