Final answer:
The function write_csv takes a dictionary and a file_name as arguments, writing a .csv file where each line corresponds to one key-value pair from the dictionary, with the key as the first element of the line and the list of values as subsequent elements.
Step-by-step explanation:
The student has requested the creation of a Python function called write_csv that generates a .csv file from a dictionary. Below is the Python code for the described function:
def write_csv(data_dict, file_name):
with open(file_name, 'w', newline='') as csv_file:
for key, values in data_dict.items():
csv_file.write(f'{key}, ' + ', '.join(map(str, values)) + '\\')
This function takes a dictionary where each key has a list of values, and a string file_name, and creates a .csv file with each key-value pair on a separate line. When calling write_csv(my_data, "stipends.csv") with the provided dictionary, the generated stipends.csv file would contain lines like "Peter, 1000" and so forth. Similarly, write_csv(my_data, "population.csv") will produce a population.csv with the appropriate data layout.