Home > Back-end >  Print All Key and Value pairs without Quotations for Keys
Print All Key and Value pairs without Quotations for Keys

Time:12-06

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:

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.

CodePudding user response:

Try this:

', '.join(['%s: %s' % item for item in data['n']['properties'].items()])

CodePudding user response:

Try this:

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

CodePudding user response:

for key, Value in data.items():
    print(f"{key} : {Value}")
  • Related