Lecture 5 in-class exercise solution

Lecture 5 in-class exercise solution#

import requests

params = {
    "api_key": "DEMO_KEY",
    "office": "P",
    "party": "DEM",
    "has_raised_funds": True,
    "election_year": 2024,
}
response = requests.get("https://api.open.fec.gov/v1/candidates/", params=params)
data = response.json()
data
{'api_version': '1.0',
 'pagination': {'count': 20,
  'is_count_exact': True,
  'page': 1,
  'pages': 1,
  'per_page': 20},
 'results': [{'active_through': 2024,
   'candidate_id': 'P00008193',
   'candidate_inactive': False,
   'candidate_status': 'N',
   'cycles': [2018, 2020, 2022, 2024],
   'district': '00',
   'district_number': 0,
   'election_districts': ['00', '00'],
   'election_years': [2020, 2024],
   'federal_funds_flag': False,
   'first_file_date': '2018-08-26',
   'has_raised_funds': True,
   'inactive_election_years': None,
   'incumbent_challenge': 'C',
   'incumbent_challenge_full': 'Challenger',
   'last_f2_date': '2022-05-22',
   'last_file_date': '2022-05-22',
   'load_date': '2023-01-12T22:24:01',
   'name': 'AHN, SHINAE MS',
   'office': 'P',
   'office_full': 'President',
   'party': 'DEM',
   'party_full': 'DEMOCRATIC PARTY',
   'state': 'US'},
  {'active_through': 2024,
   'candidate_id': 'P40013955',
   'candidate_inactive': False,
   'candidate_status': 'N',
   'cycles': [2024, 2026],
   'district': '00',
   'district_number': 0,
   'election_districts': ['00'],
   'election_years': [2024],
   'federal_funds_flag': False,
   'first_file_date': '2023-06-21',
   'has_raised_funds': True,
   'inactive_election_years': None,
   'incumbent_challenge': 'C',
   'incumbent_challenge_full': 'Challenger',
   'last_f2_date': '2023-06-21',
   'last_file_date': '2023-06-21',
   'load_date': '2023-07-10T21:05:09',
   'name': 'AYALA, ROBERT CARLOS',
   'office': 'P',
   'office_full': 'President',
   'party': 'DEM',
   'party_full': 'DEMOCRATIC PARTY',
   'state': 'US'},
  {'active_through': 2024,
   'candidate_id': 'P80000722',
   'candidate_inactive': False,
   'candidate_status': 'P',
   'cycles': [1988, 1990, 2008, 2010, 2012, 2020, 2022, 2024, 2026],
   'district': '00',
   'district_number': 0,
   'election_districts': ['00', '00', '00', '00'],
   'election_years': [1988, 2008, 2020, 2024],
   'federal_funds_flag': True,
   'first_file_date': '1987-04-03',
   'has_raised_funds': True,
   'inactive_election_years': None,
   'incumbent_challenge': 'I',
   'incumbent_challenge_full': 'Incumbent',
   'last_f2_date': '2023-09-05',
   'last_file_date': '2023-09-05',
   'load_date': '2025-03-18T15:34:07',
   'name': 'BIDEN, JOSEPH R JR',
   'office': 'P',
   'office_full': 'President',
   'party': 'DEM',
   'party_full': 'DEMOCRATIC PARTY',
   'state': 'US'},
  {'active_through': 2024,
   'candidate_id': 'P40013716',
   'candidate_inactive': False,
   'candidate_status': 'P',
   'cycles': [2024, 2026],
   'district': '00',
   'district_number': 0,
   'election_districts': ['00'],
   'election_years': [2024],
   'federal_funds_flag': False,
   'first_file_date': '2023-06-13',
   'has_raised_funds': True,
   'inactive_election_years': None,
   'incumbent_challenge': 'C',
   'incumbent_challenge_full': 'Challenger',
   'last_f2_date': '2023-06-13',
   'last_file_date': '2023-06-13',
   'load_date': '2025-03-18T15:34:07',
   'name': 'BUKOVINAC, TERRISA LIN',
   'office': 'P',
   'office_full': 'President',
   'party': 'DEM',
   'party_full': 'DEMOCRATIC PARTY',
   'state': 'US'},
  {'active_through': 2024,
   'candidate_id': 'P40010621',
   'candidate_inactive': False,
   'candidate_status': 'P',
   'cycles': [2024, 2026],
   'district': '00',
   'district_number': 0,
   'election_districts': ['00'],
   'election_years': [2024],
   'federal_funds_flag': False,
   'first_file_date': '2023-01-26',
   'has_raised_funds': True,
   'inactive_election_years': None,
   'incumbent_challenge': 'C',
   'incumbent_challenge_full': 'Challenger',
   'last_f2_date': '2023-04-20',
   'last_file_date': '2023-04-20',
   'load_date': '2025-03-18T15:34:07',
   'name': 'CAMILLERI, CHARLES WILLIAM',
   'office': 'P',
   'office_full': 'President',
   'party': 'DEM',
   'party_full': 'DEMOCRATIC PARTY',
   'state': 'US'},
  {'active_through': 2028,
   'candidate_id': 'P80000268',
   'candidate_inactive': False,
   'candidate_status': 'N',
   'cycles': [1986,
    1988,
    1990,
    1992,
    1994,
    1996,
    1998,
    2000,
    2002,
    2004,
    2006,
    2008,
    2010,
    2012,
    2014,
    2016,
    2018,
    2020,
    2022,
    2024,
    2026],
   'district': '00',
   'district_number': 0,
   'election_districts': ['00',
    '00',
    '00',
    '00',
    '00',
    '00',
    '00',
    '00',
    '00',
    '00',
    '00'],
   'election_years': [1988,
    1990,
    1992,
    2000,
    2004,
    2008,
    2012,
    2016,
    2020,
    2024,
    2028],
   'federal_funds_flag': True,
   'first_file_date': '1986-02-05',
   'has_raised_funds': True,
   'inactive_election_years': None,
   'incumbent_challenge': None,
   'incumbent_challenge_full': None,
   'last_f2_date': '2025-02-13',
   'last_file_date': '2025-02-13',
   'load_date': '2025-02-14T21:05:30',
   'name': 'CARTER, WILLIE FELIX',
   'office': 'P',
   'office_full': 'President',
   'party': 'DEM',
   'party_full': 'DEMOCRATIC PARTY',
   'state': 'US'},
  {'active_through': 2024,
   'candidate_id': 'P40007031',
   'candidate_inactive': False,
   'candidate_status': 'N',
   'cycles': [2022, 2024, 2026],
   'district': '00',
   'district_number': 0,
   'election_districts': ['00'],
   'election_years': [2024],
   'federal_funds_flag': False,
   'first_file_date': '2021-12-13',
   'has_raised_funds': True,
   'inactive_election_years': None,
   'incumbent_challenge': 'C',
   'incumbent_challenge_full': 'Challenger',
   'last_f2_date': '2021-12-13',
   'last_file_date': '2021-12-13',
   'load_date': '2023-01-12T22:24:01',
   'name': 'GAILEY, WILLIAM',
   'office': 'P',
   'office_full': 'President',
   'party': 'DEM',
   'party_full': 'DEMOCRATIC PARTY',
   'state': 'US'},
  {'active_through': 2024,
   'candidate_id': 'P00009423',
   'candidate_inactive': False,
   'candidate_status': 'P',
   'cycles': [2020, 2022, 2024, 2026],
   'district': '00',
   'district_number': 0,
   'election_districts': ['00', '00'],
   'election_years': [2020, 2024],
   'federal_funds_flag': False,
   'first_file_date': '2019-01-21',
   'has_raised_funds': True,
   'inactive_election_years': None,
   'incumbent_challenge': 'C',
   'incumbent_challenge_full': 'Challenger',
   'last_f2_date': '2024-08-06',
   'last_file_date': '2024-08-06',
   'load_date': '2025-03-18T15:34:07',
   'name': 'HARRIS, KAMALA',
   'office': 'P',
   'office_full': 'President',
   'party': 'DEM',
   'party_full': 'DEMOCRATIC PARTY',
   'state': 'US'},
  {'active_through': 2024,
   'candidate_id': 'P40016073',
   'candidate_inactive': False,
   'candidate_status': 'P',
   'cycles': [2024, 2026],
   'district': '00',
   'district_number': 0,
   'election_districts': ['00'],
   'election_years': [2024],
   'federal_funds_flag': False,
   'first_file_date': '2023-10-24',
   'has_raised_funds': True,
   'inactive_election_years': None,
   'incumbent_challenge': 'C',
   'incumbent_challenge_full': 'Challenger',
   'last_f2_date': '2023-10-24',
   'last_file_date': '2023-10-24',
   'load_date': '2025-03-18T15:34:07',
   'name': 'JOHNSON, CONSTANCE L',
   'office': 'P',
   'office_full': 'President',
   'party': 'DEM',
   'party_full': 'DEMOCRATIC PARTY',
   'state': 'US'},
  {'active_through': 2028,
   'candidate_id': 'P60016383',
   'candidate_inactive': False,
   'candidate_status': 'N',
   'cycles': [2016, 2018, 2024, 2026],
   'district': None,
   'district_number': None,
   'election_districts': ['00', None, None],
   'election_years': [2016, 2024, 2028],
   'federal_funds_flag': False,
   'first_file_date': '2015-10-01',
   'has_raised_funds': True,
   'inactive_election_years': None,
   'incumbent_challenge': None,
   'incumbent_challenge_full': None,
   'last_f2_date': '2024-12-09',
   'last_file_date': '2024-12-09',
   'load_date': '2024-12-16T21:01:00',
   'name': 'KITTINGTON, VALMA',
   'office': 'P',
   'office_full': 'President',
   'party': 'DEM',
   'party_full': 'DEMOCRATIC PARTY',
   'state': 'US'},
  {'active_through': 2024,
   'candidate_id': 'P40016024',
   'candidate_inactive': False,
   'candidate_status': 'P',
   'cycles': [2024, 2026],
   'district': '00',
   'district_number': 0,
   'election_districts': ['00'],
   'election_years': [2024],
   'federal_funds_flag': False,
   'first_file_date': '2023-10-22',
   'has_raised_funds': True,
   'inactive_election_years': None,
   'incumbent_challenge': 'C',
   'incumbent_challenge_full': 'Challenger',
   'last_f2_date': '2023-10-22',
   'last_file_date': '2023-10-22',
   'load_date': '2025-03-18T15:34:07',
   'name': 'PALMER, JASON MICHAEL MR.',
   'office': 'P',
   'office_full': 'President',
   'party': 'DEM',
   'party_full': 'DEMOCRATIC PARTY',
   'state': 'US'},
  {'active_through': 2024,
   'candidate_id': 'P40010514',
   'candidate_inactive': False,
   'candidate_status': 'P',
   'cycles': [2024, 2026],
   'district': '00',
   'district_number': 0,
   'election_districts': ['00'],
   'election_years': [2024],
   'federal_funds_flag': False,
   'first_file_date': '2023-01-19',
   'has_raised_funds': True,
   'inactive_election_years': None,
   'incumbent_challenge': 'C',
   'incumbent_challenge_full': 'Challenger',
   'last_f2_date': '2023-01-19',
   'last_file_date': '2023-01-19',
   'load_date': '2025-03-18T15:34:07',
   'name': 'PEREZ-SERRATO, ARMANDO',
   'office': 'P',
   'office_full': 'President',
   'party': 'DEM',
   'party_full': 'DEMOCRATIC PARTY',
   'state': 'US'},
  {'active_through': 2024,
   'candidate_id': 'P40016131',
   'candidate_inactive': False,
   'candidate_status': 'P',
   'cycles': [2024, 2026],
   'district': '00',
   'district_number': 0,
   'election_districts': ['00'],
   'election_years': [2024],
   'federal_funds_flag': False,
   'first_file_date': '2023-10-26',
   'has_raised_funds': True,
   'inactive_election_years': None,
   'incumbent_challenge': 'C',
   'incumbent_challenge_full': 'Challenger',
   'last_f2_date': '2023-10-26',
   'last_file_date': '2023-10-26',
   'load_date': '2025-03-18T15:34:07',
   'name': 'PHILLIPS, DEAN',
   'office': 'P',
   'office_full': 'President',
   'party': 'DEM',
   'party_full': 'DEMOCRATIC PARTY',
   'state': 'US'},
  {'active_through': 2028,
   'candidate_id': 'P00013045',
   'candidate_inactive': False,
   'candidate_status': 'N',
   'cycles': [2020, 2022, 2024, 2026],
   'district': '00',
   'district_number': 0,
   'election_districts': ['00', None, '00'],
   'election_years': [2020, 2024, 2028],
   'federal_funds_flag': False,
   'first_file_date': '2019-08-06',
   'has_raised_funds': True,
   'inactive_election_years': None,
   'incumbent_challenge': None,
   'incumbent_challenge_full': None,
   'last_f2_date': '2025-03-12',
   'last_file_date': '2025-03-12',
   'load_date': '2025-03-14T21:00:51',
   'name': 'POPE SR, WAYNE ANTHONY',
   'office': 'P',
   'office_full': 'President',
   'party': 'DEM',
   'party_full': 'DEMOCRATIC PARTY',
   'state': 'US'},
  {'active_through': 2024,
   'candidate_id': 'P00008946',
   'candidate_inactive': False,
   'candidate_status': 'P',
   'cycles': [2018, 2020, 2022, 2024, 2026],
   'district': '00',
   'district_number': 0,
   'election_districts': ['00', '00'],
   'election_years': [2020, 2024],
   'federal_funds_flag': False,
   'first_file_date': '2018-12-07',
   'has_raised_funds': True,
   'inactive_election_years': None,
   'incumbent_challenge': 'C',
   'incumbent_challenge_full': 'Challenger',
   'last_f2_date': '2022-07-15',
   'last_file_date': '2022-07-15',
   'load_date': '2025-03-18T15:34:07',
   'name': 'POWERS, CHRISTIN NOEL MS.',
   'office': 'P',
   'office_full': 'President',
   'party': 'DEM',
   'party_full': 'DEMOCRATIC PARTY',
   'state': 'US'},
  {'active_through': 2024,
   'candidate_id': 'P40021107',
   'candidate_inactive': False,
   'candidate_status': 'N',
   'cycles': [2024, 2026],
   'district': '00',
   'district_number': 0,
   'election_districts': ['00'],
   'election_years': [2024],
   'federal_funds_flag': False,
   'first_file_date': '2024-08-03',
   'has_raised_funds': True,
   'inactive_election_years': None,
   'incumbent_challenge': 'C',
   'incumbent_challenge_full': 'Challenger',
   'last_f2_date': '2024-08-03',
   'last_file_date': '2024-08-03',
   'load_date': '2024-08-22T21:00:32',
   'name': 'RAND, RACHEL LYDIA',
   'office': 'P',
   'office_full': 'President',
   'party': 'DEM',
   'party_full': 'DEMOCRATIC PARTY',
   'state': 'US'},
  {'active_through': 2024,
   'candidate_id': 'P00009225',
   'candidate_inactive': False,
   'candidate_status': 'N',
   'cycles': [2020, 2022, 2024, 2026],
   'district': '00',
   'district_number': 0,
   'election_districts': ['00', '00'],
   'election_years': [2020, 2024],
   'federal_funds_flag': False,
   'first_file_date': '2019-01-12',
   'has_raised_funds': True,
   'inactive_election_years': None,
   'incumbent_challenge': 'C',
   'incumbent_challenge_full': 'Challenger',
   'last_f2_date': '2024-07-08',
   'last_file_date': '2024-07-08',
   'load_date': '2024-07-18T21:02:01',
   'name': 'SENEY, RAYMOND J',
   'office': 'P',
   'office_full': 'President',
   'party': 'DEM',
   'party_full': 'DEMOCRATIC PARTY',
   'state': 'US'},
  {'active_through': 2024,
   'candidate_id': 'P40015752',
   'candidate_inactive': False,
   'candidate_status': 'C',
   'cycles': [2024],
   'district': '00',
   'district_number': 0,
   'election_districts': ['00'],
   'election_years': [2024],
   'federal_funds_flag': False,
   'first_file_date': '2023-10-11',
   'has_raised_funds': True,
   'inactive_election_years': None,
   'incumbent_challenge': 'C',
   'incumbent_challenge_full': 'Challenger',
   'last_f2_date': '2023-10-11',
   'last_file_date': '2023-10-11',
   'load_date': '2024-01-31T21:03:49',
   'name': 'UYGUR, CENK',
   'office': 'P',
   'office_full': 'President',
   'party': 'DEM',
   'party_full': 'DEMOCRATIC PARTY',
   'state': 'US'},
  {'active_through': 2024,
   'candidate_id': 'P40011579',
   'candidate_inactive': False,
   'candidate_status': 'N',
   'cycles': [2024, 2026],
   'district': '00',
   'district_number': 0,
   'election_districts': ['00'],
   'election_years': [2024],
   'federal_funds_flag': False,
   'first_file_date': '2023-03-26',
   'has_raised_funds': True,
   'inactive_election_years': None,
   'incumbent_challenge': 'C',
   'incumbent_challenge_full': 'Challenger',
   'last_f2_date': '2023-03-26',
   'last_file_date': '2023-03-26',
   'load_date': '2023-04-16T21:09:31',
   'name': 'WASHINGTON, JOHN III',
   'office': 'P',
   'office_full': 'President',
   'party': 'DEM',
   'party_full': 'DEMOCRATIC PARTY',
   'state': 'US'},
  {'active_through': 2024,
   'candidate_id': 'P00009910',
   'candidate_inactive': False,
   'candidate_status': 'P',
   'cycles': [2020, 2022, 2024, 2026],
   'district': '00',
   'district_number': 0,
   'election_districts': ['00', '00'],
   'election_years': [2020, 2024],
   'federal_funds_flag': False,
   'first_file_date': '2019-02-04',
   'has_raised_funds': True,
   'inactive_election_years': None,
   'incumbent_challenge': 'C',
   'incumbent_challenge_full': 'Challenger',
   'last_f2_date': '2024-07-08',
   'last_file_date': '2024-07-08',
   'load_date': '2025-03-18T15:34:07',
   'name': 'WILLIAMSON, MARIANNE',
   'office': 'P',
   'office_full': 'President',
   'party': 'DEM',
   'party_full': 'DEMOCRATIC PARTY',
   'state': 'US'}]}
[candidate["name"] for candidate in data["results"]]
['AHN, SHINAE MS',
 'AYALA, ROBERT CARLOS',
 'BIDEN, JOSEPH R JR',
 'BUKOVINAC, TERRISA LIN',
 'CAMILLERI, CHARLES WILLIAM',
 'CARTER, WILLIE FELIX',
 'GAILEY, WILLIAM',
 'HARRIS, KAMALA',
 'JOHNSON, CONSTANCE L',
 'KITTINGTON, VALMA',
 'PALMER, JASON MICHAEL MR.',
 'PEREZ-SERRATO, ARMANDO',
 'PHILLIPS, DEAN',
 'POPE SR, WAYNE ANTHONY',
 'POWERS, CHRISTIN NOEL MS.',
 'RAND, RACHEL LYDIA',
 'SENEY, RAYMOND J',
 'UYGUR, CENK',
 'WASHINGTON, JOHN III',
 'WILLIAMSON, MARIANNE']