Exercises, examples and other material relating to training module Y107. This topic is presented on public courses
Learning to program in Python,
Python Programming
Lists and tuples are collections of data values held in an object in which they can be referenced by a position number. Dictionaries are mutable collections of objects in which data values can be looked up name.
Related technical and longer articles
Python List, Python Tuple, Python Dictionary
| Articles and tips on this subject | updated |
| 3555 | Football league tables - under old and new point system. Python program. In 1981, football scoring for the top divisions of the league changed from 2 points for a win to 3 points for a win in the 1981 season [ref]. This was done to encourage positive play, and discourage boring defensive games played for a goalless draw.
How much difference would it make if the old scoring ... | 2011-12-24 |
| 3554 | Learning more about our web site - and learning how to learn about yours There are quite a number of tools out there which will give you statistics about your web site - and quite a lot of people who will tell you various statistics about yours and theirs. But there's "Lies, Damned lies and statistics" according to Benjamin Disraeli. How do you really understand your traffic ... | 2011-12-17 (longest) |
| 3488 | Python sets and frozensets - what are they? A Python set is like a dictionary, but without a value being held.
Quite often, I'll have a long list of names / places / skills / words and I want to make up a unique list - to produce a pulldown menu, for example, in which each occurs just once.
I could do that with a dictionary - either storing ... | 2011-10-20 |
| 3464 | Passing optional and named parameters to python methods When you call a named block of code (a method or function), you'll wish to pass in originating values that are changed from one call / use to the next - these are commonly known as the parameters. If I'm calling a function to calculate the area of a rectangle, for example, I would pass in the width ... | 2011-10-04 |
| 2994 | Python - some common questions answered in code examples Some tips and new examples from last week ... Python in Plymouth!
• How do I put comments in a Python regular expression to make it more readable: [source]
• How do I use a python dictionary as a table of counters - in our example, counting the number of people in our team who have each of ... | 2010-10-10 |
| 2986 | Python dictionaries - reaching to new uses If it sounds easy, it should be easy.
"Go through a file and echo the lines ... but if a line's duplicated, echo it only once". This was a sample application requested on today's Python course ... but one which has been troubling the folks for a while as they tried to filter the data in other languages.
The ... | 2010-10-05 |
| 2915 | Looking up a value by key - associative arrays / Hashes / Dictionaries In any substantial programming task, you'll want to store a whole series of values in some sort of table, so that you can process / reprocess / handle each of the elements in turn - and to do that, you'll use a variable construct that's called an "array", or a "list", a "tuple" or a "vector". (The exact ... | 2010-08-20 |
| 2368 | Python - fresh examples of all the fundamentals Some more new examples in Python - from this week's course.
From my Introduction to Python / simple example to show the power of the language, I present my example that parsed a big data (log) file and counter and sorted by number of accesses the hits from various remote hosts. A long report, ending ... | 2009-08-23 (longer) |
| 103 | Can't resist writing about Python Home, a long week, a short post. I have things to write ... ranging from Python to Pyramid organisations (wait - that's not much of a range - they're close in the dictionary ;-) - but that will have to wait until tomorrow.
But that does remind me that "Python Dictionaries" are currently the top find ... | 2008-05-11 |
| 1145 | Using a list of keys and a list of values to make a dictionary in Python - zip If you have two lists in Python and you want to use them as the keys and values of a dictionary, you might be interested on Python's zip function. It's NOT the use of zip as we know it for file compression - rather it's used to interlace (combine) elements of two lists into a list of lists.
Here's ... | 2007-04-12 |
| 1144 | Python dictionary for quick look ups Using a dictionary in Python, you can avoid the need for a loop in your program to search out elements in a collection. Suppose, for example, you have a list of people and a list of their countries of origin, you could look up individuals like this:
names = ["Jesus","Marc","Michal","Graham"]
places ... | 2007-04-12 |
| 955 | Python collections - mutable and imutable Should you use a list or a tuple ... or perhaps a dictionary to store a collection of objects in your Python program? To help you make the decision, think whether you need to be able to modify the collection after you've created it (mutable) or not (immutable), and whether you want it to be indexed ... | 2006-12-01 |
Examples from our training material
| coin.py | Class used by dictionary with object key example |
| d1.py | Defining and looking up in a dictionary |
| d2.py | Looping through all the elements of a dictionary |
| d3.py | Can I sort a dictionary? |
| dfp | Optional, list and dictionary params |
| file_2nd | Analysing blog accesses |
| marie.data | data file with duplicated lines |
| nx | Dictionary of objects - exercise and sample answer |
| pax_month.py | Unique visitors / visited pages per month |
| pocket.py | dictionary with object keys - example |
| pred.py | Illustration of a program that would benefit from dictionaries |
| pres.py | Is a key used in a disctionary? |
| pycounter | counting words using dictionary |
| pyset | listing words using set |
| slowcoach | Analysing visit lengths to web site - no cache |
| snork | Variable scope when sorting a dictionary by value |
| soccer.data | Soccer League table |
| soccer_py | Soccer - league table, old v new point system |
| topsk | counting occurrences in a file |
| u0 | report unique lines from file |
| u1 | Report each line from a file |
| u2 | count of each line in a file, sorted by count |
| uniquise | Sort all lines in a file, sort by frequency, list lines they occur on |
| visitors | Analysing visit lengths to web site - using cache |
| zip-it.py | Joining 2 lists into a dictionary |
Pictures
Practical session on a Python course
Background information
Some modules are
available for download as a sample of our material or under an
Open Training Notes License for free download from
http://www.training-notes.co.uk.
Topics covered in this module
Why do we need dictionaries?.
Defining and using a dictionary.
Hashing and Sorting.
Checking whether an itemÕs in a dictionary.
Using other objects as dictionary keys.
Complete learning
If you are looking for a complete course and not just a information on a single subject, visit our
Listing and schedule page.
Well House Consultants specialise in training courses in
Python,
Perl,
PHP, and
MySQL. We run
Private Courses throughout the UK (and beyond for longer courses), and
Public Courses at our training centre in Melksham, Wiltshire, England.
It's surprisingly cost effective to come on our public courses -
even if
you live in a different
country or continent to us.
We have a technical library of over 700 books on the subjects on which we teach.
These books are available for reference at our training centre. Also
available is the Opentalk
Forum for discussion of technical questions.