Writing a function to help you

Suppose you needed to repeat the same process done in the previous exercise to many, many rows of data. Rewriting your code again and again could become very tedious, repetitive, and unmaintainable.

In this exercise, you will create a function to house the code you wrote earlier to make things easier and much more concise. Why? This way, you only need to call the function and supply the appropriate lists to create your dictionaries! Again, the lists feature_names and row_vals are preloaded and these contain the header names of the dataset and actual values of a row from the dataset, respectively.

This exercise is part of the course

Python Toolbox

View Course

Exercise instructions

  • Define the function lists2dict() with two parameters: first is list1 and second is list2.
  • Return the resulting dictionary rs_dict in lists2dict().
  • Call the lists2dict() function with the arguments feature_names and row_vals. Assign the result of the function call to rs_fxn.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Define lists2dict()
def ____(____, ____):
    """Return a dictionary where list1 provides
    the keys and list2 provides the values."""

    # Zip lists: zipped_lists
    zipped_lists = zip(list1, list2)

    # Create a dictionary: rs_dict
    rs_dict = dict(zipped_lists)

    # Return the dictionary
    

# Call lists2dict: rs_fxn
rs_fxn = ____

# Print rs_fxn
print(rs_fxn)