Home > Enterprise >  How to get the both keys and values using python
How to get the both keys and values using python

Time:09-28

I have a data like

{
 "A":[
  {
  "key1": "value1"
  "key2": "value2"
  "key3": [
   {
   "key4": "value4"
   "key5": "value5"
   }
   ]
  }
 ]
}

How can I get the both key4 : value4 and key5 : value5 using python

CodePudding user response:

Look ↓↓↓↓↓

name_dict = {
 "A":[
  {
  "key1": "value1"
  "key2": "value2"
  "key3": [
   {
   "key4": "value4"
   "key5": "value5"
   }
   ]
  }
 ]
}
print(name_dict["A"][0]["key3"][0]["key4"], "\n", name_dict["A"][0]["key3"][0]["key5"])

CodePudding user response:

Is this what you want? It's better to paste what you have tried and what error you got so that we get a clear idea on the issue. The below code is just from what I understood from the description mentioned:

data = {
 "A":[
  {
  "key1": "value1",
  "key2": "value2",
  "key3": [
   {
   "key4": "value4",
   "key5": "value5",
   },
   ]
  }
 ]
}

print ('key4: ' data['A'][0]['key3'][0]['key4'])
print ('key5: '  data['A'][0]['key3'][0]['key5'])

Output:

key4: value4
key5: value5
  • Related