Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to use a for loop to display the type of each value stored against each key in dictionary?

person = {'Name':'Dustin','Age':19,'HasCar':True}

How can I get the following output using for loop:

The key "Name" has the value "Dustin" of the type "<class 'str'>"
  

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

use items() function to get both key and value like this

person = {'name':'Dustin','Age':19,'HasCar':True}
for key,val in person.items():
    print(f'The key "{key}" has the value "{val}" of the type "{type(val)}"')

output

The key "name" has the value "Dustin" of the type "<class ‘str’>"

The key "Age" has the value "19" of the type "<class ‘int’>"

The key "HasCar" has the value "True" of the type "<class ‘bool’>"

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading