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.
LISTS AND TUPLES
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.
DICTIONARIES
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
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
ITERATORS
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 functionSee 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