Well House Consultants
You are on the site of
Well House Consultants
Open Source Training Courses
hotel accommodation. You are welcome to browse and use
our resources subject to our copyright statement and to add in links from your pages to ours.
Other subject areas - resources
Well House Manor Resources
Object Orientation and General topics
Linux / LAMP / Tomcat Resources
Well House Consultants Resources
C and C++ Resources
Web and Intranet Resources
Python module Y107
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 articlesPython List, Python Tuple, Python Dictionary
|Articles and tips on this subject||updated|
|4029||Exception, Lambda, Generator, Slice, Dict - examples in one Python program|
A new example from last week's Python course, showing exception, lambda, generator and list slices in a practical programming example. The task we took was to go through a file of railway stations and ticket sales figures, and report on the most and least used 20 in the UK. The programs's [here].
|4027||Collections in Python - list tuple dict and string.|
All the languages that we teach have "collection" variables - single names under which a series of values is stored, keyed or indexed in some way. There are four such types in Python
Lists ... starting off with index position 0, and alterable within the stucture as the program runs. You may erroneously ...
|3934||Multiple identical keys in a Python dict - yes, you can!|
If you have a list, you can only have one element at each number - there's just a single positon , a single  and so on. That's clear, and natural to understand. With a dict in Python (like a hash in Perl or an associative array in PHP), it's peceived wisdom that you can only have one element with ...
|3662||Finding all the unique lines in a file, using Python or Perl|
A question - how do I process all the unique lines from a file in Python? Asked by a delegate today, solved neatly and easily using a generator which means that there's no need to store all the data - unique values can be passed back and processed onwards as they're found. This is fantastic news if ...
|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 ...
|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 ...
|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 ...
|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 ...
|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 ...
|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.
|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 ...
|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 ...
|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 ...
|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.
|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"]
|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 ...
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|
|setup|| Simple use of dict|
|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|
|timberpull|| multiple values at same key|
|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|
|wp2|| dict - members with same key|
|zip-it.py|| Joining 2 lists into a dictionary|
PicturesPractical session on a Python course
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.
If you are looking for a complete course and not just a information on a single subject, visit our Listing and schedule
Well House Consultants specialise in training courses in
. We run
throughout the UK (and beyond for longer courses), and
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.