Lab 10: Congress dictionary

In this part of lab, a congress dictionary is provided in JSON file format. The congress dictionary contains every member of the United States Congress who served at any point between January 1947 and Februrary 2014.

Notes:

First, let's get familiar with the congressDict dictionary. The keys are the names of the congress members, and the values are dictionaries. For example, Loretta B. Sanchez served in the House of Representatives for California from 1997 to 2017. She was first elected in 1996. Her name is the key in the dictionary, and the value associated with her name is another dictionary.

Note that this example is formatted to make the key:value pairs easy to read. Thonny, however, will display the key:value pairs on the same line.

>> congressDict['Loretta B. Sanchez']
{'houseOrSenate': 'house', 
         'state': 'CA', 
         'party': 'D', 
      'birthday': '1960-01-07'}

Here are two more examples. Joseph P. Kennedy has served as a House Representative for Massachusetts since 2013 and Elizabeth H. Dole served as a North Carolina Senator from 2003 to 2009.


>>> congressDict['Joseph P. Kennedy']
{'houseOrSenate': 'house', 
         'state': 'MA', 
         'party': 'D', 
      'birthday': '1980-10-04'}

>>> congressDict['Elizabeth H. Dole']
{'houseOrSenate': 'senate', 
         'state': 'NC', 
         'party': 'R', 
      'birthday': '1936-07-29'}

Task 1. getCongressDict

Partner B

Write a function called getCongressDict that takes the JSON file name as a parameter and returns the dictionary. You'll need this import statement at the top of your file so you can use the json library.

import json 

Hint: you'll need to use json.load to load in the json file.

Here are some sample tests of the getCongressDict function:

>>> getCongressDict('hello.json')
Oops, unable to read json file hello.json

>>> congressDict = getCongressDict('congress.json')

>>> len(congressDict)
3178

The remaining functions depend on the existence of the congressDict dictionary.

Task 2. partyList

Partner A

Write a function called partyList that takes the congress dictionary and a party (eg, 'R' for Republican, 'D' for Democrat, or 'I' for independent) and returns a list of all congress members that belong to the given party.

Here are some examples (you can scroll right in the window below to see all the names).

>>> partyList(congressDict, 'I')
['Harry Flood Byrd', 'Eugene Joseph McCarthy', 'Hubert Horatio Humphrey', 'Henry Frazier Reams', 
'obert  Humphreys', 'Walter Frederick Mondale', 'James Lane Buckley', 'James M. Jeffords', 
'Wendell Richard Anderson', 'Muriel Buck Humphrey', 'Joseph I. Lieberman', 'Bernard  Sanders', 
'Dean  Barkley', 'Angus  King']

>>> partyList(congressDict, 'D')
['Joseph Jefferson Mansfield', 'Robert Lee Doughton', 'Adolph Joachim Sabath', 'Sol  Bloom', 
'Schuyler Otis Bland', 'Louis Leon Ludlow', 'John Hosea Kerr', 'Robert  Crosser', 
'Clarence Frederick Lea', 'John  Kee', 'Brent  Spence', 'Mary Teresa Norton', ...]

Task 3. stateAndPartyList

Partner B

This is very similar to Task 2 above. Write a function called stateAndPartyList that takes the congress dictionary and a party and a state and returns a list of all congress members that belong to the given party and the given state.

Here are some examples:

>>> stateAndPartyList(congressDict, 'R', 'AK')
['Howard Wallace Pollock', 'Ted F. Stevens', 'Don E. Young', 'Frank H. Murkowski', 
'Lisa A. Murkowski']
>>> stateAndPartyList(congressDict, 'I', 'VT')
['James M. Jeffords', 'Bernard  Sanders']

>>> stateAndPartyList(congressDict, 'D', 'RI')
['Aime Joseph Forand', 'John Edward Fogarty', 'Theodore Francis Green', 'James Howard McGrath', 
'Edward Laurence Leahy', 'John Orlando Pastore', 'Fernand Joseph St. Germain', 
'Claiborne de Borda Pell', 'Robert Owens Tiernan', 'Edward Peter Beard', 'John F. Reed', 
'Patrick Joseph Kennedy', 'Robert A. Weygand', 'James R. Langevin', 'Sheldon  Whitehouse', 
'David N. Cicilline']

Task 4. buildStateDict

Partner A

You could imagine that it would be helpful to have this same data set organized by state. In this task, you'll write a function called buildStateDict that takes the congress dictionary and builds a new dictionary. In this new dictionary, the keys are states (use the two-letter abbreviation for each state, e.g. 'MA' for Massachusetts), and values are lists of tuples of people representing that state in this format: (name, party, houseOrSenate)

Notes about tuples:

Here are some buildStateDict examples:

>>> len(congressDict)
3178
>>> stateDict = buildStateDict(congressDict)
>>> len(stateDict)
50
>>> stateDict['AK']
[('Edward Lewis Bartlett', 'D', 'senate'), ('Ralph Julian Rivers', 'D', 'house'), 
('Ernest  Gruening', 'D', 'senate'), ('Howard Wallace Pollock', 'R', 'house'), 
('Ted F. Stevens', 'R', 'senate'), ('Maurice Robert Gravel', 'D', 'senate'), 
('Nicholas Joseph Begich', 'D', 'house'), ('Don E. Young', 'R', 'house'), 
('Frank H. Murkowski', 'R', 'senate'), ('Lisa A. Murkowski', 'R', 'senate'), 
('Mark  Begich', 'D', 'senate')]

>>> stateDict['VT']
[('Charles Albert Plumley', 'R', 'house'), ('Ralph Edward Flanders', 'R', 'senate'), 
('George David Aiken', 'R', 'senate'), ('Winston Lewis Prouty', 'R', 'senate'), 
('William Henry Meyer', 'D', 'house'), ('Robert Theodore Stafford', 'R', 'senate'), 
('Richard Walker Mallary', 'R', 'house'), ('James M. Jeffords', 'I', 'senate'), 
('Patrick J. Leahy', 'D', 'senate'), ('Peter P. Smith', 'R', 'house'), 
('Bernard  Sanders', 'I', 'senate'), ('Peter  Welch', 'D', 'house')]

>>> stateDict['CA']
[('Richard Joseph Welch', 'R', 'house'), ('Clarence Frederick Lea', 'D', 'house'), 
('Franck Roberts Havenner', 'D', 'house'), ('Willis Winter Bradley', 'R', 'house'), 
('Harry Richard Sheppard', 'D', 'house'), ('John  Phillips', 'R', 'house'), 
('Justin Leroy Johnson', 'R', 'house'), ('Bertrand Wesley Gearhart', 'R', 'house'),
...]

>>> len(stateDict['ME'])
29
>>> len(stateDict['NY'])
243
>>> len(stateDict['WI'])
58

Task 5. writeDictToJson

Partner B

Write a function called writeDictToJson that takes two parameters: 1) the dictionary to be stored and 2) the json filename to store the dict. You'll need to use json.dump.

>>> writeDictToJson(stateDict, 'congressStateDict.json')
# Check your lab10 folder for the new congressStateDict.json file

Task 6. buildHouseSenateCountDict

Partner A

Write a function called buildHouseSenateCountDict that takes one parameter, the congress dictionary, and returns a new dictionary. The new dictionary has two keys, house and senate. The values associated with each of the keys is itself a dictionary, where the keys of the subdictionary are the US states. The value for each state is yet another dictionary, where the parties are the keys and the counts of members of that party from that state are the values.

This is more easily seen in an example:

>>> countDict = buildHouseSenateCountDict(congressDict)

Here is a glimpse of countDict:

{ 'house': {'NY': {'D': 122, 'R': 105, 'AL': 2}
            'TX': {'D': 93, 'R': 43}, 
            'NC': {'D': 50, 'R': 24}, 
            'IL': {'D': 65, 'R': 72}, 
            'NJ': {'R': 40, 'D': 35}, 
            'KY': {'R': 18, 'D': 23},
            ...
           }
  'senate':{'WY': {'R': 10, 'D': 4}, 
            'IA': {'R': 6, 'D': 5}, 
            'MD': {'R': 4, 'D': 7}, 
            'NY': {'R': 6, 'D': 7, 'I': 1}
           ...
           }
}
>>> len(countDict)
2 # because only 2 keys, 'house' and 'senate'

>>> len(countDict['house'])
50 # 50 states

>>> len(countDict['senate'])
50 # 50 states

Some helpful questions to discuss with your partner as you talk through this function. Let's use Loretta Sanchez as our example.

>>> congressDict['Loretta B. Sanchez']
{'houseOrSenate': 'house', 
         'state': 'CA', 
         'party': 'D', 
      'birthday': '1960-01-07'}

Table of Contents