Lab 8, Part 2: Student dictionaries

Create a new file called At the top of your file, add this line so you can access the studentDict dictionary:

from studentDict import studentDict

In the file, we are providing you with a randomly-generated CS111 student dictionary called studentDict. The dictionaries are entirely made up. You can open the file in canopy and look at the contents. Here is a peek at some of the dictionary contents. In this lab, we will practice manipulating a dictionary to create new dictionaries.

            'Eunji': {'major': 'Spanish', 
                      'color': 'chartreuse', 
                      'movie': 'Avengers: End Game', 
                       'year': '2024'},

            'Amal':  {'major': 'Political Science', 
                      'color': 'yellow', 
                      'movie': 'Parasite', 
                       'year': '2021'},

           'Jocelyn':{'major': 'Philosophy', 
                      'color': 'aquamarine', 
                      'movie': 'Da 5 Bloods', 
                       'year': '2021'},


Task 1A. Build a Majors dictionary.

Write a function called makeMajorsCountDict that takes a dictionary parameter (eg studentDict) returns a dictionary with majors as the key and count of students with that major as the value.

>>> majorsDict = makeMajorsCountDict(studentDict)
>>> majorsDict
{          'Physics': 10, 
               'MAS': 10, 
           'History': 11, 
        'Philosophy': 11, 
          'Japanese': 4, 
                'CS': 7, 
              'CAMS': 5, 
           'Spanish': 8, 
'East Asian Studies': 1, 
              'Math': 10, 
         'Economics': 3, 
               'Bio': 12, 
           'English': 5, 
 'Political Science': 4}

Task 1B. Create list of tuples from majorsDict

Write a function called extractTups that takes a dictionary and returns a list of tuples in the form (major, count). Dictionaries do not have order, so the tuples will come out unordered.

Challenge: use a list comprehension to generate your list of tuples

Note Your list of tuples may display differently. We are formatting the lists to maximize readability in this page.

>>> majorTups = extractTups(majorsDict)
>>> majorTups

[('Physics', 10), ('MAS', 10), ('History', 11), 
  ('Philosophy', 11), ('Japanese', 4), ('CS', 7), 
  ('CAMS', 5), ('Spanish', 8), ('East Asian Studies', 1), 
  ('Math', 10), ('Economics', 3), ('Bio', 12), 
  ('English', 5), ('Political Science', 4)]

Task 1C. Sort the tuples

Task a. Sort tuples alphabetically by major

A short aside about SORTING
There are two built-in python functions (click on each word for documentation): sorted and sort. In a nutshell, sorted returns a new sorted list, whereas sort changes the contents of the original list into a sorted list. When you use sort, the order of the original list is lost. We do not want to change our lists here, so sorted is the best one to use here.

[('Bio', 12), ('CAMS', 5), ('CS', 7), 
 ('East Asian Studies', 1), ('Economics', 3), ('English', 5), 
 ('History', 11), ('Japanese', 4), ('MAS', 10), ('Math', 10), 
 ('Philosophy', 11), ('Physics', 10), 
 ('Political Science', 4), ('Spanish', 8)]

Task b. Sort tuples numerically by count

Hint: a helper function is a good idea here. Example 3 on this page describes key functions.

[('East Asian Studies', 1), ('Economics', 3), 
 ('Japanese', 4), ('Political Science', 4), 
 ('CAMS', 5), ('English', 5), ('CS', 7), 
 ('Spanish', 8), ('Physics', 10), ('MAS', 10), 
 ('Math', 10), ('History', 11), 
 ('Philosophy', 11), ('Bio', 12)]

Task 2. Color dictionary with student list values

Write a function called makeColorDict that takes a dictionary parameter (e.g. studentDict) and returns a dictionary with the color as the key and a list of the students' names with that color as the value.

For example, here are the students who have the color red. This is not the entire dictionary, just a glimpse of one value for one key (you can scroll to the right in the window below to see the entire list of students who love red).

   'blue': [ # list of student names here ],

   'red': ['Sarah', 'Emilia', 'Rumi', 'Keiko', 'Youngja', 'Daisy', 'Trisha', 'Seana', 'Bryant'],

   'plaid': [ # list of student names here ],

Task 3. Nested dictionaries.

Write a function called makeColorMiniDict that takes a dictionary parameter and returns a dictionary with the color as the key and a list of dictionaries as the value. Each dictionary in the list of dictionaries represents one student who has that color.

Here is a snapshot of the list of dictionaries that is the value associated with the key value red (you can scroll horizontally <--> to see all the dictionaries).

    'blue': [ #list of dictionaries here ],

    'red': [{'movie': 'Black Panther', 'major': 'MAS', 'year': '2023', 'name': 'Sarah'}, 
            {'movie': 'Frozen', 'major': 'CS', 'year': '2023', 'name': 'Emilia'}, 
            {'movie': 'Da 5 Bloods', 'major': 'Bio', 'year': '2022', 'name': 'Rumi'}, 
            {'movie': 'Avengers: End Game', 'major': 'English', 'year': '2024', 'name': 'Keiko'}, 
            {'movie': 'Parasite', 'major': 'MAS', 'year': '2024', 'name': 'Youngja'}, 
            {'movie': 'Black Panther', 'major': 'History', 'year': '2023', 'name': 'Daisy'}, 
            {'movie': 'Avengers: End Game', 'major': 'English', 'year': '2024', 'name': 'Trisha'}, 
            {'movie': 'Avengers: End Game', 'major': 'Physics', 'year': '2024', 'name': 'Seana'}, 
            {'movie': 'Star Wars', 'major': 'Bio', 'year': '2021', 'name': 'Bryant'}],

    'plaid': [ # list of dictionaries here ],

Table of Contents