Python Tuple – Another Python Collection

There are lists, dictionaries, and tuples. These are all Python collections.

A Python tuple is like a non-changeable list. Instead of the square brackets that are used for lists, for tuples you use the regular parenthesis.

You are not able to use many of the methods on a Python tuple, because they are immutable. For example, you can not use sort, reverse, or append.

You can use the dir function to check what you can do with tuples, compared to lists.

Python tuple versus list
Compare what you can do with a Python tuple versus a list.

Why would you use tuples if they are not as capable as lists? You would use a Python tuple because it is more efficient, and they require less memory. You should use Python tuples when creating a collection that is temporary.

Two Way TuplesA nice thing about Python is you can do two things in one by placing a tuple on both the left and right side of an assignment statement.

Two Way Tuples 2Please note, the left-hand side must contain variables. Also, you can omit parenthesis on the left-hand side.

If you remember, the ‘items’ method for a Python dictionary returns a (key, value) pair. This pair is a Python tuple, so you can use a tuple as an iteration variable to loop through a dictionary.

Another nice thing about tuples is they are comparable. Comparison operators work with tuples. The first element will be compared first. If they are equal, then Python will move to the next element. It stops when it finds elements that differ.

Comparable Tuples

Compare and Sort a Python Tuple

This ability to compare Python tuples is a nice feature, because things that can be compared can also be sorted. You can use the built-in sorted function to do this, like in the following example.

#Create a dictionary. A dictionary can not be sorted.
>>>d = {‘alpha’:5, ‘charlie’:3, ‘beta’:4}
#Assign to variable x a sorted list of tuples.
>>>x = sorted(d.items())
[(‘alpha’, 5), (‘beta’, 4), (‘charlie’, 3)]


Notice it only sorts the key. You can loop through this to print in sorted key order.

>>>for k, v in sorted(d.items()):
… print k, v

alpha 5
beta 4
charlie 3


Do you remember finding the most common word program? What if you want to find the five most common words? Rather than sort through the key, you will want to sort the value in descending order.

#Create a dictionary. A dictionary can not be sorted.
>>>d = {‘alpha’:5, ‘charlie’:3, ‘beta’:4}
#Creat a temporary list.
>>>temp_list = list()
#Loop through the dictionary, but append the value first!
>>>for k, v in d.items():
… temp_list.append( (v,k) )

>>>print temp_list
[(3, ‘charlie), (4, ‘beta’), (5, ‘alpha’)]
#Reverse the sorted order of values.
>>>print temp_list
[(5, ‘alpha’), (4, ‘beta’), (3, ‘charlie’)]


The following is a program that will find the ten most common words in a text file.

10 most common words program
A Python program for finding the 10 most common words.

Once you become comfortable with this program, you can begin to understand ways to condense your code. The concept of list comprehension can make a dynamic list in one line.

# Start with the dictionary.
>>> d = {‘alpha’:5, ‘charlie’:3, ‘beta’:4}
# Use list comprehension to make a dynamic list.
>>> print sorted( [(v, k) for k, v in d.items()] )
[(3, ‘charlie), (4, ‘beta’), (5, ‘alpha’)]


The syntax inside the parenthesis serves as the list comprehension. It dynamically creates a list of the pair (v, k) as it itereates through the key, value pairs inside the dictionary. This syntax is rather dense, but you can use it as you become more comfortable programming in Python.

Leave a Reply

Your email address will not be published. Required fields are marked *