python-two-list-to-dict
To convert two lists into a dictionary, you can use the zip() function in Python. The zip() function takes two or more lists and returns an iterator that aggregates elements from each list.
Here's an example:
keys = ['apple', 'banana', 'cherry']
values = [1, 2, 3]
my_dict = dict(zip(keys, values))
print(my_dict)
Output:
{'apple': 1, 'banana': 2, 'cherry': 3}
In this example, we first created two lists, keys and values, that contain the keys and values we want to use in our dictionary. We then used the zip() function to combine these lists into a single iterator of tuples. Finally, we used the dict() function to convert this iterator into a dictionary.
原文地址: https://www.cveoy.top/t/topic/rFm 著作权归作者所有。请勿转载和采集!