how can I convert a tuple into a key value pairs dynamically?
Let’s say I have:
tuple = ('name1','value1','name2','value2','name3','value3')
I want to put it into a dictionary:
dictionary = { name1 : value1, name2 : value2, name3 : value3 )
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Convert the tuple to key-value pairs and let the
dictconstructor build a dictionary:The
zip(it, it)idiom produces pairs of items from an otherwise flat iterable, providing a sequence of pairs that can be passed to thedictconstructor. A generalization of this is available as the grouper recipe in the itertools documentation.If the input is sufficiently large, replace
zipwithitertools.izipto avoid allocating a temporary list. Unlike expressions based on mappingt[i]to[i + 1], the above will work on any iterable, not only on sequences.