How to convert nested tuple to dictionary in Python

2 Answers

0 votes
tpls = ((4, 'php'), (9, 'c'), (3, 'java'))

result = [{'key': sub[0], 'value': sub[1]} for sub in tpls]

print(result)




'''
run:

[{'key': 4, 'value': 'php'}, {'key': 9, 'value': 'c'}, {'key': 3, 'value': 'java'}]

'''

 



answered Dec 16, 2023 by avibootz
0 votes
tpls = ((4, 'php'), (9, 'c'), (3, 'java'))

keys = ['key', 'value'] 
 
result = [{key: val for key, val in zip(keys, sub)} for sub in tpls] 

print(result)




'''
run:

[{'key': 4, 'value': 'php'}, {'key': 9, 'value': 'c'}, {'key': 3, 'value': 'java'}]

'''

 



answered Dec 16, 2023 by avibootz

Related questions

2 answers 114 views
...