Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,844 questions

51,765 answers

573 users

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 355 views
1 answer 216 views
4 answers 289 views
1 answer 197 views
1 answer 128 views
2 answers 259 views
...