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
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 Y111
More on Collections and Sequences
Exercises, examples and other material relating to training module Y111. This topic is presented on public courses Learning to program in Python
, Python Programming
, Intermediate Python
Lists, tuples and dictionaries are fundamental building blocks of Python. This module takes a look at some of the more advanced features of the use of these collection objects, assuming a prior knowledge of all the fundamental aspects of Python.
Related technical and longer articlesUser defined sorting - what is a callback?Python List, Python Tuple, Python Dictionarycopying an object - copy the reference
|Articles and tips on this subject
|Mutable v Immuatble objects in Python, and the implication
In python, "everything is an object" - well, at least everything you name, and lots of standard system things too, are!
Some object types are immutable. That means tha once they have been created, they cannot be modified and any code that looks like it modifies them in reallity produces a new object ...
|Accessing variables across subroutine boundaries - Perl, Python, Java and Tcl
In a perfect programming world, code is broken into named blocks (methods, functions, subroutines, classes, procedures, commands, modules, packages etc) and data is passed around between the blocks purely as parameters and return values. And variables are local to the block in which they're used. This ...
|zip in Python
Zipping has become synonymouse with compressing data in programming terms; originally the use of the word in that sense came from zipping up a bag so that a whole lot of contents were held together, but these days the compression (pushing everything into the bag) is kind of expected.
Python's zip function ...
|Python for loops - applying a temporary second name to the same object
If you write a for loop in Python to go through each element of a list, you assign an extra (temporary) name to each member of the list while within the loop. So than any changes made to the named variable will also be changes to the object in the original list. However, that's overshaddowed / overtaken ...
|List slices in Python - 2 and 3 values forms, with an uplifting example
Python's lists are indexed collection objects. That means that they're rather like arrays in that you look up elements by their position numbers, and the number start at a fixed point (0 in the case of Python); they're not totally like arrays in that they are not stored at unchanging sequential memory ...
|Python dictionaries - mutable and immutable keys and values
Lists, Tuples, and dictionaries are the conventional collection variables in Python - but when you stop to consider it, objects and strings are collections too. All of these structures bundle together other elements (members) in various ways.
In first dictionary demonstrations, we usually use strings ...
|Copying - duplicating data, or just adding a name? Perl and Python compared
When you copy a list in Perl, you're duplicating the data and you end up with two distinct copies ... but when you copy a list in Python, you're copying the reference so that you end up with two names for the same variable - almost like an alias.
So in Perl - with two different copies - you end up with ...
|Sorting - naturally, or into a different order
There's a natural sort order for many things - for numbers, it's ascending, for words it's a dictionary order, for names it's by surname. But sometimes you want to sort a list of things different, or there's no provided way within a programming language to apply the natural sort to your type of thing.
|Sorting people by their names
Please sort into order:
Uncle Tom Cobbley
What would you do?
Uncle Tom Cobbley
That's not a standard sort order for strings of text ... and if you've got ...
|Python - extend v append on a list
In Python, you can extend a list and you can append to it as well.
What's the difference? If you append a list to another list, you add the new list as a single extra list to the original, thus makingthe original list just one longer with an item that is itself a list. But if you extend a list ...
|Python - access to variables in the outer scope
In Python, variables are local to the block in which they're used unless declares in some other way. And that's good news, because the last thing you want in a substantial script is for data to "leak" between functions as can happen in default-accepting Perl or Lua.
But there is an exception ... if ...
|List Comprehensions in Python
How do you perform an operation on every member of a list, producing a new list? A List Comprehension in Python is a structure in which a for loop is written within a list's square brackets. Its purpose is to allow the programmer to write short code that's used to transform each element of a source list, ...
|Anonymous functions (lambdas) and map in Python
Why do you name variables? So that you can use them again later. But if you don't want to use them more than once, why bother with a name at all? Most programming languages create temporary or anonymous variables within a single line, and if you've programmed almost anything, you'll have used them without ...
|Callbacks - a more complex code sandwich
When you write a piece of code, you're normally putting the filling into the sandwich; there's a built-in program in your computer that controls the loading and running of the code thst you've written, and there is a whole library of standard pieces of code that you call to perform the low level operations. ...
|Last elements in a Perl or Python list
How do we refer to the elements of a list? By index number, starting at zero and stopping one short of the number of elements in the list. So a 20 element list has element numbers 0 to 19.
How can we refer to the last element, then? We could write an expression based on the length of the list - ...
|Copying a reference, or cloning
If you copy a variable in a program, you end up with a duplicate, right?
set second $first ... in Tcl
second=$first ... in shell
second = first ... in Python, Ruby, C and Java
$second = $first ... in Perl and PHP
Well - ALMOST right. For sure an assignment copies a variable, but where that variable ...
|Python is a fabulous language
I'm running a Python course this Wednesday-through-Friday and for students who have self-taught themselves just enough of a structural programming language, it's a big challenge and also an opening of a door through which a bright light shines. Even I'm still amazed by the cleanness of it all and the ...
Examples from our training material
| List - multiple dimensions
| Collections - initial setup and reference
| List - copy through assignment
| alias, shallow copy and deep copy comparison
| using a callback to map a list
| List - deep copy
| Python equivalent of Schwartzian transform
| A list of lists (a matrix?)
| Further methods that run on lists - looking things up
| Sorting a dictionary?
| copy reference and shallow copy
| List - revision
| Natural sorting of a list of strings
| Sample answer - module exercise
| Sorting and subsorting into your own order
| Sorting in a non-standard way
| Sorting by surname
| Encapsulating changes in a function
| List - shallow copy
| list slices - revision
| Counting in a dictionary, and sorting
| Sorting of a list of strings - user routine ignoring case
PicturesTo teach Python in Manchester
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
Assignments, shallow and deep copies.
Further operations on lists.
Sorting a list - natural order and user sorts.
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.