Is there any build-in function in Python that merges two lists into a dict? Like:
combined_dict = {}
keys = ["key1","key2","key3"]
values = ["val1","val2","val3"]
for k,v in zip(keys,values):
combined_dict[k] = v
Where:
keys
acts as the list that contains the keys.
values
acts as the list that contains the values
There is a function called array_combine that achieves this effect.
The simplest way to merge two dictionaries in python is by using the unpack operator(**). By applying the "**" operator to the dictionary, it expands its content being the collection of key-value pairs.
Dictionaries in Python First, a given key can appear in a dictionary only once. Duplicate keys are not allowed.
If you want to keep duplicate keys in a dictionary, you have two or more different values that you want to associate with same key in dictionary. The dictionary can not have the same keys, but we can achieve a similar effect by keeping multiple values for a key in the dictionary.
Seems like this should work, though I guess it's not one single function:
dict(zip(["key1","key2","key3"], ["val1","val2","val3"]))
from here: How do I combine two lists into a dictionary in Python?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With