Reputation: 29
This folowing returns
{'Name': 'Value1'}
but I am looking for {'Name': 'Value1', 'Value2'}
. Any help would be appreciated, cheers.
a = ["Name"]
b = "Value1 Value2"
c=b.split()
d=dict(zip(a, c))
Upvotes: 0
Views: 377
Reputation: 977
I'm not 100% how you want to build this dict, mainly because your code does not return {'Name': 'V'}
, it returns {'Name': 'Value1'}
.
Dictionaries store one-to-one mappings between keys and values. That means that an entry such as {'Name': 'Value1', 'Value2'}
would not be valid in a dictionary.
The workaround that most people use (and what you appear to be trying to do based on your code) is to store lists as values instead. However, your code misuses the zip()
builtin. One thing that would work would be
a = ["Name"]
b = "Value1 Value2"
c=b.split()
d=dict(zip(a,[c])) # Zip two single-element lists instead of zipping
# lists of different size
but as I said before, I'm not 100% sure if this is what you're looking for. I hope it helps though!
Upvotes: 0
Reputation: 459
Your dictionary is not correctly formatted. You should have a tuple of values as a value of the dictionary:
a = ["name"]
b = [("Value1", "Value2")]
d = dict(zip(a,b))
Upvotes: 1