Question: Define a function named create_region_dictionary(records_list) that takes a list of tuples containing a year, a region and a number of births as a parameter and

Define a function named create_region_dictionary(records_list) that takes a list of tuples containing a year, a region and a number of births as a parameter and returns a tuple containing a list and a dictionary. The list to be returned will consist of years from the parameter list. The key of each item in the dictionary is the region (e.g. "Auckland region") and the value is a list of tuples where the first element is the year and the second element is the number of births in that region in the given year. For example, if the parameter list contains the following data:

[(2022, 'Northland region', 2367), (2022, 'Auckland region', 20469), (2021, 'Northland region', 2262), (2021, 'Auckland region', 20520)]

Then the function returns the tuple below:

([2021, 2022], {'Northland region': [(2021, 2262), (2022, 2367)], 'Auckland region': [(2021, 20520), (2022, 20469)]})

The returned list contains year 2021 and year 2022. The key of the first dictionary item is 'Northland region' and the value is a list of tuples where 2021 is the year and 2262 is the number of births of 'Northland region' in 2021.

Note:

You can assume that the parameter list is not empty.

The returned list of years in the tuple must be in ascending order.

The list of tuples in the returned dictionary must be in ascending order. You may find the sort() method useful. After sorting, the first tuple in the example above will be (2021, 2262) and the second tuple will be (2022, 2367) in 'Northland region' item.

For example:

Test Result
data = [(2022, 'Northland region', 2367), (2022, 'Auckland region', 20469), (2021, 'Northland region', 2262), (2021, 'Auckland region', 20520)] a_list, a_dict = create_region_dictionary(data) print(a_list) for key in sorted(a_dict): print(key, a_dict[key])
[2021, 2022] Auckland region [(2021, 20520), (2022, 20469)] Northland region [(2021, 2262), (2022, 2367)]

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!