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

Print All Key and Value pairs without Quotations for Keys

I have this nested dictionary in Python:

data={
    "n": {
"identity": 25,
"labels": [
        "Transaction"
      ],
"properties": {
"date": 20141023,
"amount": 3890.36,
"currency": "USD",
"time": "6:09"
      }
    }
  }

I want to print out the keys and values for "properties" in a single line, but without the quotations for keys, like this:

date : 20141023 , amount : 3890.36, currency: "USD", time: "6:09"

So far, I can only print out the keys without the quotes (except for the final key), but not sure how to get the values printed as well:

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

for key,value in data.items():
    print("{}".format(":".join(value.get('n').get('properties'))))

Output:

date: amount: currency: time

Not really experienced with all this yet so I’d appreciate any help you guys could give me on this. Thank you.

>Solution :

Try this:

', '.join(['%s: %s' % item for item in data['n']['properties'].items()])
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