A tuple is like a list. It is an ordered sequence but unlike a list it is immutable. It is created by specifying the items in the tuple within parentheses and separated by commas.
coord = (7.3, 2.9) employee_record = ('john', 'doe', '123 Any Street', 'Austin', 'TX')Tuples have many uses like specifying the (x, y) coordinates of a point in Cartesian space or employee records from a database. You may not assign to individual elements in a tuple. You can extract individual elements from a tuple by what is known as sequence unpacking.
x, y = coord first, last, street, city, state = employee_recordIn general, use a tuple instead of a list when you know that you will not be changing the elements in it. Going from a list to a tuple and vice versa is fairly simple:
a = [1, 2, 3, 4] b = tuple (a) # b = (1, 2, 3, 4) p = ('a', 'b', 'c') q = list (p) # q = ['a', 'b', 'c']
A set is an unordered collection of unique objects. You can create a set from a list. If you do all duplicate elements are eliminated and the ordering is lost.
a = [1, 1, 2, 2, 2, 3, 4, 4, 4, 4] b = set (a) # b = set([1, 2, 3, 4])A set supports the mathematical operations of union, intersection, difference, and symmetric difference.
# define two sets a = set ('abracadabra') # a = set(['a', 'r', 'b', 'c', 'd']) b = set ('alacazam') # b = set(['a', 'c', 'z', 'm', 'l']) # Union of the two sets c = a | b # c = set(['a', 'c', 'b', 'd', 'm', 'l', 'r', 'z']) # Intersection of the two sets d = a & b # d = set(['a', 'c']) # Difference of the two sets (letters in a but not in b) g = a - b # g = set(['r', 'b', 'd']) # Symmetric difference letters in a or b but not both h = a ^ b # h = set(['b', 'd', 'm', 'l', 'r', 'z']) # To get the number of elements in a set num_elements = len (a) # To iterate over the elements in a set for elt in a: print (elt) # To convert a set into a list p = list (a)
A dictionary is an unordered collection of key-value pairs. The keys are unique and have to be immutable types like numbers or strings. In other languages a dictionary is called an associative array, hash, or map. There are two ways you can create a dictionary. You can start with an empty dictionary and add key-value pairs or you can enumerate key-value pairs. The syntax for doing so is shown below:
phone_book = {} phone_book['Mickey Mouse'] = '409-896-4861' phone_book['Donald Duck'] = '409-896-5007' phone_book = {'Mickey Mouse':'409-896-4861', 'Donald Duck':'409-896-5007'}Since keys are unique, you can always extract the value by using the key as the index. However, you cannot get the key easily if you just know the value, since values are not unique. You can also assign values if you know the key. If the key does not exist, a new key-value pair is created in the dictionary. If the key already exists, the old value gets over-written. To iterate through all the entries in a dictionary you can do:
for key in dict: print (dict[key])
There are several built-in functions that makes it easy to manipulate a dictionary.
Function | Meaning |
---|---|
dict.has_key (key) | Returns True if the dictionary contains the specified key and False otherwise. |
key in dict | Returns True if the dictionary contains the specified key and False otherwise. |
dict.keys() | Returns a list of keys. |
dict.values() | Returns a list of values. |
dict.items() | Returns a list of tuples representing key-value pairs. |
dict.get (key, default) | If the dictionary has the key it returns the value, otherwise it returns the default value. |
del dict[key] | Deletes the specified entry. |
dict.clear() | Deletes all entries. |