4

I am trying to convert a line of string to dictionary where i am facing an error. here is what i have and what i did:

line="nsd-1:quorum"
t=tuple(line.split(":"))
d=dict(t)
Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    d=dict(t)
ValueError: dictionary update sequence element #0 has length 5; 2 is required

Basically, what i want to achieve is to have a key value pair. So if i have set of values separated by a ":", i want to have it as a key whatever is before the colon and after the colon needs to be the value for the key. example: if i take the above string, i want "nsd-1" as my key and "quorum" as value.

Any help is appreciated. Thanks

2 Answers 2

9

Wrap it in a list:

>>> dict([t])
{'nsd-1': 'quorum'}

There's also no need to convert the return value of split to a tuple:

>>> dict([line.split(':')])
{'nsd-1': 'quorum'}
Sign up to request clarification or add additional context in comments.

Comments

4

Put t inside an empty list, like this:

d=dict([t])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.