Click here to Skip to main content
15,892,697 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Define and implement a function that converts the a list of tuples to a dictionary where the first value of each tuple is the key, and the second value is the dictionary value.

For example, if the user enters the following list: [('Person1', 80), ('Person2', ), ('Person3', 20), ('Person4', 20, 60)]

Then the output should be: {'Person1': 80, 'Person2': None, 'Person3': 20, 'Person4': 20}

What I have tried:

I have tried a few codes but it's giving me an error. I'd appreciate it if you can write the code. Thank you in advance.
Posted
Updated 13-Mar-22 21:27pm

1 solution

Try
Python
lin = [('Person1', 80), ('Person2', ), ('Person3', 20), ('Person4', 20, 60)]
dout = { t[0] : t[1] if len(t) > 1 else None for t in lin}
print(dout)
 
Share this answer
 
Comments
Maciej Los 14-Mar-22 5:46am    
5ed!
BeginnerCoder1 14-Mar-22 19:02pm    
CPallini, Brilliant. Much appreciated :)
CPallini 15-Mar-22 2:57am    
You are welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900