Lab 10, Part 2: Student dictionaries

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

from studentDict import studentDict

In the file studentDict.py, 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).

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

A short aside about SORTING
There are two built-in python functions (click on each word for documentation): sorted and sort (there's also a brief tutorial on sorting that covers advanced details). 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.

To keep things organized, add code to your extractTups function that sorts the results before returning them. By default, this will end up sorting based on the alphabetical ordering of the first letter of each major:

[('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 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 ],
     ...
 }

Optional Challenge: Multiple layers.

If you've still got time left in the lab, write a function called makeMajorClassDict that organizes the data into a dictionary > dictionary > list-of-dictionaries structure where the outer keys are majors, the inner keys are class years, and the final values are lists of full student dicts (still including major and class year).

Here's an example of what part of that might look like:

{
    'Bio': { # dictionary here },
    'English': {
        '2024': [
            {'movie': 'Avengers: End Game', 'major': 'English', 'year': '2024', 'name': 'Trisha'},
            {'movie': 'Avengers: End Game', 'major': 'English', 'year': '2024', 'name': 'Keiko'},
        ],
        '2023': [
            {'movie': 'Black Panther', 'major': 'English', 'year': '2023', 'name': 'Sarah'},
        ]
    },
    'Physics': { # dictionary here },
    ...
}

Table of Contents