Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))

Well House Consultants
You are on the site of Well House Consultants who provide Open Source Training Courses and business 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
Java Resources
Well House Manor Resources
Perl Resources
Python Resources
PHP Resources
Object Orientation and General topics
MySQL Resources
Linux / LAMP / Tomcat Resources
Well House Consultants Resources
Extras Resources
C and C++ Resources
Ruby Resources
Tcl/Tk Resources
Web and Intranet Resources
Python module Y107
Dictionaries
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 subjectupdated
4668Sorting a dict in Python
* You cannot sort a dict * If you sort a list of strings that are digits, you don't get a numeric sort by default Solutions * Sort a list of keys * Specifiy a sort lambda (Python 2 comparator, Python 3 key function) for that list Example code:   stuff = {'9': 36, '10': 26, '8': 25, '6': ...
2016-04-02
 
4661Unique word locator - Python dict example
If a word occurs only once in my blog - all 4661 entries so far - chances are that it's a mis-spelling. And using a dict in Python, I can quickly parse a data stream with lots of text in it, isolate individual words, and see how many times each occurs. Dictionaries are a very quick and easy way of ...
2016-03-08
 
4469Sorting in Python 3 - and how it differs from Python 2 sorting
Sorting has changed between Python 2 and Python 3 ... and it's easy and logical to sort lists in Python 3. Problem is that the Python 3 documentation shows you really complicated examples ... There are two ways of sorting a list in Python 3: a) You can use the sorted function, into which you pass a ...
2015-04-22
 
4409Setting up and using a dict in Python - simple first example
Here's a very short and easy example of the use of a dict in Python. Set up a dict and fill it from a file (key value is 2nd field; value to be looked at is 7th)   lookup = {}   for record in open("railstats.txt","r"):     fields = record.strip().split('\t')     tlc ...
2015-01-30
 
4029Exception, 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]. a) ...
2013-03-04
 
4027Collections 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 ...
2013-03-04
 
3934Multiple 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 [0], a single [1] 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 ...
2012-11-25
 
3662Finding 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 ...
2012-03-24
 
3555Football 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
 
3554Learning 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)
3488Python 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
 
3464Passing 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
 
2994Python - 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
 
2986Python 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
 
2915Looking 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
 
2368Python - 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)
103Can'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
 
1145Using 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
 
1144Python 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
 
955Python 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
counter   counting server accesse
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?
d3_3.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
od   dict with digit strings as keys
od3   dict with digit strings as keys (python 3)
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 dictionary?
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
ss1.py   Python 3 - sorting with sorted
ss2.py   Python 3 - sorting a list
ssold.py   Python 2.7 - sorting a list
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
uniwords   finding words by their count in a data file
visitors   Analysing visit lengths to web site - using cache
wp2   dict - members with same key
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 [here].
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 Ruby, Lua, 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.


You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2024: 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/resources/Y107.html • PAGE BUILT: Sun Oct 11 14:50:09 2020 • BUILD SYSTEM: JelliaJamb