A Beginner_s Python Tutorial-Tuples, Lists, Dictionaries
A Beginner_s Python Tutorial-Tuples, Lists, Dictionaries
Contents
Lists are what they seem - a list of values. Each one of them is numbered, starting from zero -
the first one is numbered zero, the second 1, the third 2, etc. You can remove values from the
list, and add new values to the end. Example: Your many cats' names.
Tuples are just like lists, but you can't change their values (immutable in programmer-speak).
The values that you give it first up, are the values that you are stuck with for the rest of the
program. Again, each value is numbered starting from zero, for easy reference. Example: the
names of the months of the year.
Dictionaries are similar to what their name suggests - a dictionary. In a dictionary, you have an
'index' of words, and for each of them a definition. In Python, the word is called a 'key', and the
definition a 'value'. The values in a dictionary aren't numbered - they aren't in any specific order,
either - the key does the same thing. (Each key must be unique, though!) You can add, remove,
and modify the values in dictionaries. Example: telephone book.
Tuples
Tuples are pretty easy to make. You give your tuple a name, then after that the list of values it will
carry. For example, the months of the year:
Code Example 1 - creating a tuple
months = ('January','February','March','April','May','June',\
'July','August','September','October','November','December')
Note that the '\' thingy at the end of the first line carries over that line of code to the next line. It is
useful way of making big lines more readable. Technically, you don't have to put those parentheses
there (the '(' and ')' thingies), but it stops Python from getting things confused. You may have spaces
after the commas if you feel it necessary - it doesn't really matter. Python then organises those
values in a handy, numbered index - starting from zero, in the order that you entered them in. It
would be organised like this:
Table 1 - tuple indicies
Index Value
0 January
1 February
2 March
3 April
4 May
5 June
6 July
7 August
8 September
9 October
10 November
11 December
Lists
Lists are extremely similar to tuples. Lists are modifiable (or 'mutable', as a programmer may say),
so their values can be changed. Most of the time we use lists, not tuples, because we want to easily
change the values of things if we need to.
Lists are defined very similarly to tuples. Say you have FIVE cats, called Tom, Snappy, Kitty, Jessie
and Chester. To put them in a list, you would do this:
Code Example 2 - Creating a list
As you see, the code is exactly the same as a tuple, EXCEPT that all the values are put between
square brackets, not parentheses. Again, you don't have to have spaces after the comma, but it
does make it more readable.
You recall values from lists exactly the same as you do with tuples. For example, to print the name
of your 3rd cat you would do this:
Code Example 3 - Recalling items from a list
print(cats[2])
You can also recall a range of examples, like above, for example - cats[0:2] would return your
1st and 2nd cats, or said another way, this expression would return the zero-th element and the one-
th, or first, element, and would stop at two-th element. The output would look like this:
>>> ['Tom', 'Snappy']
Where lists come into their own is how they can be modified. To add a value to a list, you use
the append() function. Let's say you got a new cat called Catherine. To add her to the list you'd do
this:
Code Example 4 - Adding items to a list
cats.append('Catherine')
That's a little weird, isn't it? I'll explain. That function is in a funny spot - after a period, after the list
name. You'll get to see those things more in a later lesson. For the meanwhile, this is the form of the
function that adds a new value to a list:
Code Example 5 - Using the append function
You've just removed the 2nd cat in your list - poor old Snappy.
And with that morbid message, lets move on to...
Dictionaries
Ok, so there is more to life than the names of your cats. You need to call your sister, mother, son,
the fruit man, and anyone else who needs to know that their favourite cat has run away. For that you
need a telephone book.
Now, the lists we've used above aren't really suitable for a telephone book. You need to know a
number based on someone's name - not the other way around, like what we did with the cats. In the
examples of months and cats, we gave the computer a number, and it gave us a name. This time we
want to give the computer a name, and it give us a number. For this we need dictionaries.
So how do we make a dictionary? Put away your binding equipment, it isn't that advanced.
Remember, dictionaries have keys, and values. In a phone book, you have people's names, then
their numbers. See a similarity?
When you initially create a dictionary, it is very much like making a tuple or list. Tuples have ( and )
things, lists have [ and ] things. Guess what! dictionaries have { and } things - curly braces. Here is
an example below, showing a dictionary with four phone numbers in it:
Code Example 7 - Creating a dictionary
The program would then print Lewis Lame's number onscreen. Notice how instead of identifying the
value by a number, like in the cats and months examples, we identify the value, using another value
- in this case the person's name.
Ok, you've created a new phone book. Now you want to add new numbers to the book. What do you
do? A very simple line of code:
Code Example 8 - Adding entries to a dictionary
All that line is saying is that there is a person called Gingerbread Man in the phone book, and his
number is 1234567. In other words - the key is 'Gingerbread Man', and the value is 1234567.
You delete entries in a dictionary just like in a list. Let's say Andrew Parson is your neighbour, and
shot your cat. You never want to talk to him again, and therefore don't need his number. Just like in
a list, you'd do this:
Code Example 9 - Removing entries from a dictionary
Again, very easy. the 'del' operator deletes any function, variable, or entry in a list or dictionary (An
entry in a dictionary is just a variable with a number or text string as a name. This comes in handy
later on.)
Remember that append function that we used with the list? Well, there are quite a few of those that
can be used with dictionaries. Below, I will write you a program, and it will incorporate some of those
functions in. It will have comments along the way explaining what it does.
Type this program into Python IDLE (you can skip the comments). Experiment as much as you like
with it. Type it where you see the lines beginning with >>>
Code Example 10 - Functions of dictionaries
#Put it in a list:
values = ages.values()
print(values)
print(sorted(values))