Python, Lua and Tcl - public course schedule [here]
Private courses on your site - see [here]
Please ask about maintenance training for Perl, PHP, Java, C, C++, Ruby, MySQL and Linux / Tomcat systems
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 articlesPython List, Python Tuple, Python DictionaryUser defined sorting - what is a callback?copying an object - copy the reference
|Articles and tips on this subject||updated|
|4442||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 ...
|4398||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 ...
|3797||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 ...
|3439||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 ...
|3348||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 ...
|3150||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 ...
|2996||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 ...
|2920||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.
|2894||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 ...
|899||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 ...
|2718||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 ...
|1873||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, ...
|1869||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 ...
|1310||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. ...
|1304||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 - ...
|633||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 ...
|386||What is a callback?|
When you write a program, you're usually providing the filling for a sandwich.
At the top level, you have the operating system and the compiler or interpreter for the particular language you're using and this provides you with the mechanism to start and end your program, and for it to share the resources ...
Examples from our training material
|2dli.py|| List - multiple dimensions|
|coldem|| Collections - initial setup and reference|
|coplist.py|| List - copy through assignment|
|copy_levels|| alias, shallow copy and deep copy comparison|
|cuber|| using a callback to map a list|
|deepcop.py|| List - deep copy|
|dsd|| Python equivalent of Schwartzian transform|
|fortan|| A list of lists (a matrix?)|
|furlist.py|| Further methods that run on lists - looking things up|
|indemo|| Sorting a dictionary?|
|lat.py|| copy reference and shallow copy|
|lirev.py|| List - revision|
|namsort.py|| Natural sorting of a list of strings|
|p162|| Sample answer - module exercise|
|rails_dict|| Sorting and subsorting into your own order|
|sal|| Sorting in a non-standard way|
|sby_sname|| Sorting by surname|
|scenario|| Encapsulating changes in a function|
|shallowcop.py|| List - shallow copy|
|slices|| list slices - revision|
|uip2.py|| Counting in a dictionary, and sorting|
|unamsort.py|| 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.