Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
For 2021 - online Python 3 training - see ((here)).

Our plans were to retire in summer 2020 and see the world, but Coronavirus has lead us into a lot of lockdown programming in Python 3 and PHP 7.
We can now offer tailored online training - small groups, real tutors - works really well for groups of 4 to 14 delegates. Anywhere in the world; course language English.

Please ask about private 'maintenance' training for Python 2, Tcl, Perl, PHP, Lua, etc.
Python List, Python Tuple, Python Dictionary

If a Python list were like a pencil, then a Python Tuple would be like a pen.

Most programming languages include structures in which you can hold a collection of values. The most frequent example you'll come across is called an "array" - but exactly what that does varies from language to language as a C array is very different to a Tcl array, for example.

Python has FOUR (or five?) object types built into the language that can be regarded as collections.


Both LISTS and TUPLES consist of a number of objects which can be referenced by their position number within the object. I can create a list by writing
     salad = ["Lettuce","Tomato","Onion","Tuna"]
and then refer to salad[0] to refer to lettuce and salad[2] to refer to Onion. I can create a tuple by writing
     fruit = ("Apple","Banana","Cherry","Fig","Grapefruit")
and then refer to fruit[0] to refer to Apple, and so on. You'll notice that when I created the list I used [...] notation, and when I created the tuple I used (....) notation, but when I refer to elements later I do so using square brackets every time.

So what's the difference? With a list, I can change the elements, add extra elements, and so on, after it has been created. It's perfectly acceptable for me to write
     salad[3] = "Cheese"
for example if I want to replace the tuna. But if I write
     fruit[3] = "Orange"
I will get an error message when the program runs. You can change a list but NOT a tuple! In technical jargon, a tuple is immutable by a list is mutable.

Link - complete example using a list Link - complete example using a tuple
You're probably wondering why the two structures are provided - are they both necessary? A Tuple is much more efficient in use, and there are many occasions you want to set up a collection of objects in order (an "ordered collection") and refer to it many times later on. These days, though, with all the extra power of computers since Guido wrote Python, perhaps we could do without the tuple.


If you don't want to refer to the elements of a collection by their position number but prefer some other type of object as a key, you can use a python dictionary. Let's see an example of one of those:
    starter = {"John":"soup", "James":"pate", "Joan":"soup"}
There are three elements in that dictionary, and I can refer to starter["James"] and get the string "pate". You'll notice that I set up a dictionary with { ... } (curly braces, but yet again I refer to it using square brackets. If you want to perform an operation on element of a dictionary, you can find out the keys using the keys method
    print starter.keys()
for example since you can't know what the elements are called just be looking at a count of the number of elements as you can with a list or tuple.

If you're familiar with Perl hashes, PHP associative arrays or Tcl arrays, then you have a good parallel to a python dictionary.

Link - complete example using a dictionary

Strings can be treated as immutable ordered collection objects. In other words, I can write
    something = "Marmalade"
    third = something[2]
and have the letter "r" assigned to the variable third. This is a very useful facility in certain applications - for example if you're using Python in a bioinformatics application and you're doing sequence analysis.

Link - complete example using a string as a tuple

An iterator is an object that generates a collection. If you set up a large collection (for example a list that contains all ten thousand lines from a data file), you're occupying computer memory ... and if the only thing you then do with it is process each element once, it's not efficient. Increase the number of lines in the file by a factor of 10 or 100 and you're likely to find that you're starting to create "issues" on the host computer. Iterators overcome this problem. For example
    for line in huge.readlines():
will set up a temporary list containing all the data in the stream opened in object huge, then put each element into the variable line one by one. In contrast, if you write
    for line in huge.xreadlines():
then you're using an iterator and lines from the file are only read in as required.

If you want to write your own generator function, you can do so; to return an intermediate value, use the keyword yield rather than the keyword return.

Link - complete example using a generator function

See also Python programming course

Please note that articles in this section of our web site were current and correct to the best of our ability when published, but by the nature of our business may go out of date quite quickly. The quoting of a price, contract term or any other information in this area of our website is NOT an offer to supply now on those terms - please check back via our main web site

Related Material

Python - Lists and Tuples
  [383] - ()
  [657] - ()
  [899] - ()
  [955] - ()
  [1220] - ()
  [1641] - ()
  [1789] - ()
  [2280] - ()
  [2284] - ()
  [2368] - ()
  [2719] - ()
  [2996] - ()
  [3118] - ()
  [3181] - ()
  [3257] - ()
  [3348] - ()
  [3669] - ()
  [3763] - ()
  [4027] - ()
  [4368] - ()
  [4722] - ()

Python - Dictionaries
  [103] - ()
  [955] - ()
  [1144] - ()
  [1145] - ()
  [2368] - ()
  [2915] - ()
  [2986] - ()
  [2994] - ()
  [3464] - ()
  [3488] - ()
  [3554] - ()
  [3555] - ()
  [3662] - ()
  [3934] - ()
  [4027] - ()
  [4029] - ()
  [4409] - ()
  [4469] - ()
  [4661] - ()
  [4668] - ()

Python - More on Collections and Sequences
  [61] - ()
  [386] - ()
  [633] - ()
  [899] - ()
  [1304] - ()
  [1310] - ()
  [1869] - ()
  [1873] - ()
  [2718] - ()
  [2894] - ()
  [2920] - ()
  [2996] - ()
  [3150] - ()
  [3348] - ()
  [3439] - ()
  [3797] - ()
  [4398] - ()
  [4442] - ()

resource index - Python
Solutions centre home page

You'll find shorter technical items at The Horse's Mouth and delegate's questions answered at the Opentalk forum.

At Well House Consultants, we provide training courses on subjects such as Ruby, Lua, Perl, Python, Linux, C, C++, Tcl/Tk, Tomcat, PHP and MySQL. We're asked (and answer) many questions, and answers to those which are of general interest are published in this area of our site.

Comment: "Thanks for your comment, "Anon". This article is an ..."
Visitor Ranking 3.0 (5=excellent, 1=poor)

Comment by Graham (published 2010-09-11) Suggested link.
Thanks for your comment, "Anon". This article is an overview comparison and not a definitive specification of lists, tuples and dictionaries. I never claimed it to be complete reference material ... so it's natural that it's incomplete. My suggested link includes an alternative description of Tuples, described on their own rather than based on a comparison. [#3774]

Comment by Anon (published 2010-09-11)
you have an incomplete understanding of python tuple [#3689]

You can Add a comment or ranking or edit your own comments

Average page ranking - 3.0

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

PAGE: http://www.wellho.net/solutions/python-p ... onary.html • PAGE BUILT: Wed Mar 28 07:47:11 2012 • BUILD SYSTEM: wizard