How to convert a list of tuples into a dictionary with Python

3 Answers

0 votes
def Convert(tmp, dic): 
    dic = dict(tpl) 
    
    return dic 
      
  
tpl = [("aaa", 1), ("bbb", 2), ("ccc", 3), ("ddd", 4), ("eee", 5)] 

dic = {} 

print(Convert(tpl, dic)) 



'''
run:

{'aaa': 1, 'bbb': 2, 'ccc': 3, 'ddd': 4, 'eee': 5}

'''

 



answered Dec 15, 2019 by avibootz
0 votes
tpl = [("aaa", 1), ("bbb", 2), ("ccc", 3), ("ddd", 4), ("eee", 5)] 
 
dic = dict(tpl)

 
print(dic)
 
 
 
'''
run:
 
{'aaa': 1, 'bbb': 2, 'ccc': 3, 'ddd': 4, 'eee': 5}
 
'''

 



answered Jan 29, 2020 by avibootz
0 votes
lst_tpl =  [('a', 'python'), ('b', 'c#'), ('c', 'php'), ('d', 'c++')] 
  
dic =  {lst_tpl[i][0]: lst_tpl[i][1] for i in range(0, len(lst_tpl), 1)} 
 
  
print(dic)
  


     
 
'''
run:
 
{'a': 'python', 'b': 'java', 'c': 'php', 'd': 'c++'}

'''

 



answered Jan 11, 2021 by avibootz

Related questions

4 answers 385 views
1 answer 243 views
4 answers 320 views
1 answer 209 views
1 answer 148 views
2 answers 296 views
...