I'm running in a bit of trouble trying to get a script to work as a function.
The code below basically zips but for more than one list of values. So I can keep on adding list4
, list5
, etc.
list1 = ['USA', 'France', 'China']
list2 = [30, 39, 28]
list3 = [59.2, 93.1, 922.1]
dictionary = {}
for i in range(len(list1)):
d[list1[i]] = list2[i], list3[i]
print(dictionary)
I'm looking for an output like:
{'USA': (30, 59.2), 'France': (39, 93.1), 'China': (28, 922.1)}
Now I want to turn that into a function, such as:
def zipMultiple(*values, keys):
dictionary = {}
for i in range(len(keys)):
dictionary[keys[i]] = values[i]
return dictionary
myDict = zipMultiple(list2, list3, keys=list1)
I get this error:
dictionary[keys[i]] = values[i] IndexError: tuple index out of range
I'm aware that it's probably due to assigning values[i]
as though it was one list but I don't know how exactly to generalize it to say "create a tuple for every values
(list) given as an argument.