Data Science Pathway Hurricane Analysis - zip function with **kwargs

Hi all,

I was hoping to get some insight into why the code below does not output as expected. In the function I’m creating keyword arguments that should take in any number of lists when calling the function - I’m finding that when I try and zip the keyword arguments, when it zips it, rather than combining each element of each list in tuples, it creates a tuple for every list entirely. So for instance the expected result should be:

zip(list_1, list_2) → [(list_1[0], list_2[0]), (list_1[1], list_2[1]…]

instead of which I’m getting [(list_1), (list_2)]

In the commented out code, I’ve essentially done the same function, with the only difference being it zip directly takes each list as an argument, rather than from keyword arguments. This outputs correctly. Can anyone explain why there is a difference between the two methods, and how I would get it to output correctly when calling from inside the function? Any help would be appreciated!

Thank you

The issue with your code is that you are not correctly passing the lists to the function. When you are calling the function, you are passing the lists as keyword arguments, but the function is expecting them to be passed as positional arguments.

You should change the function call to the following:

function_four = hurricane_dict(names, months, years, max_sustained_winds, areas_affected, deaths)

The other issue is that your zipping code is incorrect. You should be using the following code instead:

zipped_dict = list(zip(names, months, years, max_sustained_winds, areas_affected, deaths))

This will correctly zip the lists together and create a tuple for each element in each list.

hope this helps! :slight_smile:

Hi, thanks for the response!

One final question - is there any way this can work while passing through kwargs? The reason I wanted to use kwargs is so that when calling the function I could see if I could pass in any number of lists that I wanted.

Thanks again!

The issue with the original code is that the lists were not being correctly passed to the function. Instead of using keyword arguments, the lists should be passed as positional arguments.

To fix the zipping problem, replace the listed_list and zipped_dict lines with the following:

zipped_dict = list(zip(names, months, years, max_sustained_winds, areas_affected, deaths))

This will correctly zip the lists together and create a tuple for each element in each list.

If you still want to use keyword arguments to pass the lists to the function, you can modify the function to accept them using the **lists syntax. Here’s an example:

def hurricane_dict(names, **lists):
    hurricanes = {}
    zipped_dict = list(zip(*lists.values()))
    counter = 0
    for name in names:
        hurricanes[name] = zipped_dict[counter]
        counter += 1
    return hurricanes

This modified function expects the names argument to be passed as a positional argument, while the lists are passed as keyword arguments. The **lists syntax means that any additional keyword arguments passed to the function will be collected into a dictionary called lists .