Problem

Python

The following is the name list of all people in a hypothetical class.

Christian-Andrew Bagby-wright
Matthew Chrysler
Niyousha Davachi
Pauline Dredger
Marcos Guillen
Lauren Kuffel
Shashank Kumbhare
Hany Mahdy
Sarah Moorman
Andrew Myers
Joshua Osborne
Rebecca Proni
Amir Shahmoradi
Carolina Vedovato

We want to create a dictionary of members of this class with individuals’ full names serving as dictionary keys, and their roles in class as the corresponding values of the keys. The naive way of generating this dictionary would be to use one of the methods for generating dictionary discussed in the lecture notes. For example,

class_dict  = { 
            , 'Christian-Andrew Bagby-wright' : 'student'
            , 'Matthew Chrysler'              : 'student'
            , 'Niyousha Davachi'              : 'student'
            , 'Pauline Dredger'               : 'student'
            , 'Marcos Guillen'                : 'student'
            , 'Lauren Kuffel'                 : 'student'
            , 'Shashank Kumbhare'             : 'student'
            , 'Hany Mahdy'                    : 'student'
            , 'Sarah Moorman'                 : 'student'
            , 'Andrew Myers'                  : 'student'
            , 'Joshua Osborne'                : 'student'
            , 'Rebecca Proni'                 : 'student'
            , 'Amir Shahmoradi'               : 'instructor'
            , 'Carolina Vedovato'             : 'student'
            }

However, the problem is that, except Amir, the instructor, everyone else has a student role in this class. So it would be a very painful process to type all ‘student’ values by hand manually. Now, the goal is to come up with a shortcut method that avoids the aforementioned problem.

Can you find a way of creating this dictionary, without having to type the value ‘student’ 14 times? Note that to achieve this, you don’t need anything beyond what you learned in the lecture notes. (Hint: Have a look at the section for simultaneous assignments in the lecture notes.)

Solution

Python
class_dict = { 'amir shahmoradi'  : 'instructor'
           }
class_dict
{'amir shahmoradi': 'instructor'}
class_dict[ 'Christian-Andrew Bagby-wright' ] = \
class_dict[ 'Matthew Chrysler'              ] = \
class_dict[ 'Niyousha Davachi'              ] = \
class_dict[ 'Pauline Dredger'               ] = \
class_dict[ 'Marcos Guillen'                ] = \
class_dict[ 'Lauren Kuffel'                 ] = \
class_dict[ 'Shashank Kumbhare'             ] = \
class_dict[ 'Hany Mahdy'                    ] = \
class_dict[ 'Sarah Moorman'                 ] = \
class_dict[ 'Andrew Myers'                  ] = \
class_dict[ 'Joshua Osborne'                ] = \
class_dict[ 'Rebecca Proni'                 ] = \
class_dict[ 'Carolina Vedovato'             ] = 'student'
class_dict
{'amir shahmoradi': 'instructor',
'Christian-Andrew Bagby-wright': 'student',
'Matthew Chrysler': 'student',
'Niyousha Davachi': 'student',
'Pauline Dredger': 'student',
'Marcos Guillen': 'student',
'Lauren Kuffel': 'student',
'Shashank Kumbhare': 'student',
'Hany Mahdy': 'student',
'Sarah Moorman': 'student',
'Andrew Myers': 'student',
'Joshua Osborne': 'student',
'Rebecca Proni': 'student',
'Carolina Vedovato': 'student'}
type(class_dict)
dict

Later on, we will learn more effective methods of constructing this dictionary using for-loops in Python.

Comments