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 do I get a key and its value from a dictionary?

I have a dictionary, and I want to get a key & value SEPERATED out of it:

values = {
    "a": 123,
    "b": 173,
    "c": 152,
    "d": 200,
    "e": 714,
    "f": 71
}

Is there a way to do something like this:

dictValue = values[2] #I know this will be an error
dictKey = "c"
dictValue = 152

Thanks!

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

>Solution :

Create an auxiliary dictionary with integer keys:

d = dict(enumerate(values.items()))
dictKey, dictValue = d[2]
#('c', 152)
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