Aan de slagGa gratis aan de slag

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.

Deze oefening maakt deel uit van de cursus

Python Toolbox

Cursus bekijken

Oefeninstructies

  • 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.

Praktische interactieve oefening

Probeer deze oefening eens door deze voorbeeldcode in te vullen.

# 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)
Code bewerken en uitvoeren