Saturday, 30 September 2017

Tuples - a fixed alternative to lists

Tuples are a data type in Python that are in many ways similar to lists. The big difference is that tuples cannot be changed without recreating the data object. Like strings they are immutable and cannot be changed in-place - they can only be changed by reassigning the variable name to a new value.
Tuples in code look like lists, but use round brackets () rather than square ones []
For example on the Python command line:
>>> xtuple = (5, 7, 9, 11, 13)
>>> print(type(xtuple))
<class 'tuple'>
>>> xlist = list(xtuple)
>>> print (xlist)
[5, 7, 9, 11, 13]
>>> for y in xtuple:
 print (y)

5
7
9
11
13

>>> xtuple = xtuple + (15, 17)
>>> print (xtuple)
(5, 7, 9, 11, 13, 15, 17)
>>> ytuple = tuple(xlist)
>>> print (ytuple)
(5, 7, 9, 11, 13)

>>> ztuple = (34, 10, 15, 11, 75, 3)
>>> sorted(ztuple)
[3, 10, 11, 15, 34, 75]
>>> print (ztuple)
(34, 10, 15, 11, 75, 3)
>>> ztuple = tuple(sorted(ztuple))
>>> print (ztuple)
(3, 10, 11, 15, 34, 75)
>>> if 11 in ztuple:
   print ('11 is found')

11 is found
>>> ztuple.count(15)
1
>>> ztuple.index(34)
4
>>>
 print(ztuple[0])3
This shows a number of things:
The list() function can convert a tuple to a list, and the tuple() function can convert a list to a tuple.
Tuples, like lists, ranges and even strings, can be used in a for loop.
Tuples can be concatenated, like strings and lists.
Tuples can be searched using in, like lists
Tuples can use count() function and index() function like lists
You can find the value of an element at a given index by tuplename[index] like lists

However, there are a lot of functions that lists can do that a tuple won't do.
You can't use a sort() function on a tuple (though there are ways around it as shown above)
You can't append() onto a tuple.
You can't use clear() to remove all elements from a tuple as you can with lists.

So why use tuples? Mainly because it is less easy to change them accidentally.
Here is an example script:
#!/usr/bin/python3
mondaystaff = ['Bob', 'Charlie', 'Eric']
tuesdaystaff = ['Alice', 'Charlie', 'Daria']
wednesdaystaff = ['Alice', 'Bob', 'Eric']
thursdaystaff = ['Bob', 'Charlie', 'Daria']
fridaystaff = ['Alice', 'Daria', 'Eric']
weekday = (('Monday', mondaystaff), ('Tuesday', tuesdaystaff), ('Wednesday', wednesdaystaff), ('Thursday', thursdaystaff), ('Friday', fridaystaff))
for day in weekday:
    print ('On', day[0], 'the office is manned by')
    staffstring = ''
    for staff in day[1]:
        if staff != day[1][-1]:
            staffstring = staffstring + staff + ', '
        else:
            staffstring = staffstring + 'and ' + staff
    print (staffstring)
and here is the output:
 RESTART: C:/Users/John/Dropbox/Misc Programming/Python/python3/test05_tupledemo.py On Monday the office is manned by
Bob, Charlie, and Eric
On Tuesday the office is manned by
Alice, Charlie, and Daria
On Wednesday the office is manned by
Alice, Bob, and Eric
On Thursday the office is manned by
Bob, Charlie, and Daria
On Friday the office is manned by
Alice, Daria, and Eric

>>>  
This has lists within tuples within an overall tuple. The weekday variable holds a tuple containing tuples. These are unlikely to change - the strings for days of the week are fixed, and they should be associated with the list for that day of the week.
However, the lists of strings (for the staff on each day of the week) are mutable.
This has unexpectedly turned into a demonstration of referring to collections within collections (here lists within tuples). The line
if staff != day[1][-1]:
uses day[1][-1], which is the Python way of saying find the second element in this, and then find the last element in whatever that is - i.e. it assumes that not only day is an indexed collection (i.e. list or tuple), but also day[1] is also a list or tuple and Python should find the last element [-1] of that. 
I don't know if I've explained that clearly. I hope so. And I feel I could have got the commas right but I'm not sure how.

No comments:

Post a Comment