the dictionary. This operation will result in an exception if there is no entry that has the specified key value. The “get” operation uses the .get() method of the dictionary object to return a value that corresponds to the specified key value in the dictionary. The difference between this and the second operation in the table is that this can specify a default value if there is no matching key value in the dictionary and does not result in an exception if the interpreter cannot find a corresponding key value. The .pop() method is also a method of the dictionary object and is like the .get() method, except that it has the additional feature of removing the entry with the specified key value from the dictionary when it is employed. The .update() method is also a method of the dictionary object, and we use it to make changes to and/or add entries in the dictionary.
Table 3.4
The Python code in Figure 3.19 illustrates the use of several of these dictionary operations using two different dictionary objects. Lines 2 and 3 of the code in Figure 3.19 construct a dictionary based on the GSS data that contains a few specific survey years and the number of respondents from our data set represented in tuples. Storing these data as tuples illustrates that dictionary values can take many forms and has the added advantage of protection against modifying the values due to the fact that tuples are immutable. The code in line 4 creates a second dictionary using the dict operation. The “iterable” that creates this dictionary is a set of three tuples, each being a pair of values that will become key-value pairs in the dictionary. Lines 5 and 6 print out the dictionaries, and the first two lines of output in Figure 3.20 show that the first dictionary has two key-value pairs, where the keys are strings and the values are tuples of three elements. The second dictionary has three key-value pairs where the keys and values are all individual numeric values.
Figure 3.19 Dictionary Operations
Line 8 of the code in Figure 3.19 uses the dictionary .update() method to add the entries from the years dictionary to the gss_respondents dictionary. Line 9 prints out the gss_respondents dictionary, and the labeled line of output shows that there are now five entries in the dictionary (the original two entries along with the three entries from the years dictionary). Line 11 uses a dictionary operation to assign a different value (45) to the dictionary entry with the key value 2014. Line 12 of the code then prints out the dictionary, and the labeled line of output shows the updated value for the year 2014 entry. Line 13 of the code in Figure 3.19 uses the .get() method to retrieve the value for 1992, but as there is not an entry with that key in the dictionary, the output shows that the default value “no value” is printed (and a KeyError exception is avoided). Line 14 uses the .pop() method to retrieve the value for 1991 and reports its value in the output to be 21, and line 15 prints out the dictionary, revealing that there is no longer an entry in the dictionary with a key value 1991. Line 17 of the code uses the .update() method once again to add the values from the years dictionary to the gss_respondents dictionary, and this time two changes occur. The output from printing the dictionary in line 18 shows that from the update operation, the value corresponding to the year 2014 is now 43, and a key value 1991 has been added to the dictionary with a value of 21.
Figure 3.20 Output from Dictionary Operations
SCU 3.7 Dictionary Operations
Download the file “SCU 3_7.py” from the companion website and save it either on your computer or on a removable storage device. Open the file in the Python IDLE editor and modify the indicated line of code to print just the list of the values in the dictionary using the .values() method. Execute the modified program after the change to verify that the revised code runs and produces the correct result.
Lessons learned: In this section, we learned what a dictionary is and how we can define one. We also learned about a number of operations that are used to work with dictionaries to do things like returning information corresponding to a dictionary entry or adding or changing a dictionary entry.
Example Using Tuples and Dictionaries
The following example illustrates the usefulness of both tuples and dictionaries in an example using data from the GSS. As described in Chapter 1 when we introduced the GSS data set, many of the fields in the data set are coded. Dictionaries are very useful when working with coded data to look up the meaning of specific codes. The Python code in Figure 3.21 also demonstrates the usefulness of tuples when passing multiple arguments to a function.
Figure 3.21 Tuple and Dictionary Example
The execution of the Python code in Figure 3.21 begins on line 14, as the code that precedes that is a function that we only execute when we invoke it using Python code statements. The code in line 14 creates a tuple of two elements, which are codes corresponding to the data fields “Region” and “Happy” from the GSS. The code in line 15 prints out the value of the variable codes, which is a tuple shown in the first line of output in Figure 3.22. Line 16 calls the code_lookup function with the codes variable as an argument and then assigns the function result to the response variable. It is at this point in the code execution that the flow of control transfers to the code_lookup function starting in line 3. Line 4 unpacks the tuple into two variables, region and happy. Line 5 uses a print statement to display the values of those two variables, shown in the second line of output in Figure 3.22. These variables result from the tuple that the function received as an argument, but they are not tuples themselves but rather individual values. Lines 6 to 9 and 10 to 11 create two dictionaries used in line 12. Line 12 uses a return statement to pass back to the code that called this function two values. The first value is the value found in the region_dict dictionary corresponding to the region key value. The second value is the value in the happy_dict dictionary corresponding to the happy key value. When the flow of control returns to line 16, the interpreter creates a variable (a tuple) through the assignment of the two returned values. This code in line 17 prints out the value of the result variable and in the third line of output in Figure 3.22. Finally, lines 18 and 19 print the individual values from indexing the tuple. Note in line 18 that the index value of 0 references the first value, and in line 19, the index value of 1 references the second value in the tuple. The output in the last two lines of Figure 3.22 shows the results of these two lines of code.
Figure 3.22 Output from Tuple and Dictionary Example
Dictionaries will resurface later in Chapter 5 when we talk more about structured data, and we will see tuples again as some packages have functions or objects with methods that either require parameters to be tuples or return results in tuples.
Lessons learned: In this section, we demonstrated using both tuples and dictionaries alongside each other in our code. We learned that tuples can be very useful for passing multiple values to a function and that dictionaries