Below are the two lists. Write a Python program to convert them into a dictionary in a way that item from list1 is the key and item from list2 is the value
keys = ['Ten', 'Twenty', 'Thirty'] values = [10, 20, 30]
Expected output:
{'Ten': 10, 'Twenty': 20, 'Thirty': 30}
Hint:
Use the zip() function. This function takes two or more iterables (like list, dict, string), aggregates them in a tuple, and returns it.
zip()
Or, Iterate the list using a for loop and range() function. In each iteration, add a new key-value pair to a dict using the update() method
update()
Solution1:
The zip() function and a dict() constructor
dict()
zip(keys, values)
keys = ['Ten', 'Twenty', 'Thirty'] values = [10, 20, 30] res_dict = dict(zip(keys, values)) print(res_dict)
Solution2:Using a loop and update() method of a dictionary
keys = ['Ten', 'Twenty', 'Thirty'] values = [10, 20, 30] # empty dictionary res_dict = dict() for i in range(len(keys)): res_dict.update({keys[i]: values[i]}) print(res_dict)
total answers (1)
start bookmarking useful questions and collections and save it into your own study-lists, login now to start creating your own collections.
Hint:
Use the
zip()
function. This function takes two or more iterables (like list, dict, string), aggregates them in a tuple, and returns it.Or, Iterate the list using a for loop and range() function. In each iteration, add a new key-value pair to a dict using the
update()
methodSolution1:
The zip() function and a
dict()
constructorzip(keys, values)
to aggregate two lists.zip()
function into adict()
constructor.Solution2:Using a loop and
need an explanation for this answer? contact us directly to get an explanation for this answerupdate()
method of a dictionary