Tuples, Sets and Dictionaries, Oh My!

Some more ways to store many items together



Lists and Strings are two examples of sequence data types in Python. Another sequence data type is the tuple.

A tuple is similar to a list in some ways - it is values separated by commas.

Example:
>>> t = ('a', 'b', 1)
>>> t[2]
1
>>> t[1:]
('b', 1)
>>> t2 = 4, 'x', 5, 9   # Tuple packing - values 4, 'x', 5, 9 are packed together in a tuple
>>> t2
(4, 'x', 5, 9)
>>> t2[0]
4



We can also carry out sequence unpacking, and store the values in a tuple in variables:
>>> tup = (1, 2, 3)
>>> x, y, z = tup  # unpacking the tuple
>>> x
1
>>> y
2



Note: Like strings, tuples are immutable. We cannot modify a tuple item, or add and delete items from a tuple.




Sets



set - an unordered collection with no duplicates


Python includes standard mathematical operations on sets such as union, intersection, and difference.



Example:
>>> l = ['apple', 'mango', 'peach', 'pineapple']    # list
>>> fruit = set(l)   # convert l to a set called fruit
>>> fruit
set(['mango', 'apple', 'peach', 'pineapple'])
>>> 'mango' in fruit    # membership testing
True
>>> 'pear' in fruit
False
>>> moreFruit = set(['apple', 'peach', 'plum'])
>>> moreFruit
set(['plum', 'apple', 'peach'])
>>> fruit - moreFruit    # set difference
set(['mango', 'pineapple'])




Example: More set operations
>>> s = set([1, 5, 6, 7])
>>> t = set([2, 3, 5, 6])
>>> s - t
set([1, 7])
>>> s | t    # union - elements in one or both sets
set([1, 2, 3, 5, 6, 7])
>>> s & t   # intersection - elements that are in both sets
set([5, 6])
>>> s ^ t   # symmetric difference - elements in one, but not both, sets
set([1, 2, 3, 7])




Dictionaries



Dictionaries are called maps in some languages. A dictionary is an unordered collection of key-value pairs. The key must be an immutable type, and keys must be unique in the dictionary (i.e., no two pairs in a dictionary can have the same key).


Create a dictionary by containing key-value pairs inside curly braces:
>>> dict = {}   # empty dictionary
>>> dict
{}
>>> dict = {'hammer': 'a blunt instrument for pounding nails', 'ham': 'a meat that comes from pigs'}
>>> dict['apple'] = 'a delicious fruit'
>>> dict
{'hammer': 'a blunt instrument for pounding nails', 'ham': 'a meat that comes from pigs', 'apple': ' a delicious fruit'}



To delete a key:value pair from the dictionary, use the del command and specify the key:
>>> del dict['ham']  # remove the pair associated with key 'ham' from dict
>>> dict
{'hammer': 'a blunt instrument for pounding nails', 'apple': 'a delicious fruit'}



You can overwrite the value associated with a key:
>>> dict['apple'] = 'a putrid fruit that is usually green or red'
>>> dict
{'hammer': 'a blunt instrument for pounding nails', 'apple': 'a putrid fruit that is usually green or red'}



Testing for membership of a key:
>>> 'apple' in dict
True



Form a list of the keys in a dictionary:
>>> k = dict.keys()
>>> k
['hammer', 'apple']



To iterate over a dictionary:
>>>for kee in dict:
...       print kee, ":", dict[kee]

hammer : a blunt instrument for pounding nails
apple : a putrid fruit that is usually green or red



More dictionary operations:

Operation
Description
dict.has_key(key)
returns True if dictionary contains specified key and False otherwise
dict.values()
returns a list of values in the dictionary
dict.items()
returns a list of tuples, where each tuple contains a key and value pair
dict.get(key, default)
returnsvalue associated with key if it exists, otherwise returns default value
del dict[key]
deletes the pair with specified key from the dictionary
dict.clear()
removes all pairs from the dictionary
key in dict
returns True if key is in dict, False otherwise