I am new to the concept of dictionary in Python. I have this current dictionary that contain another dictionary :
{
"count" : 3,
"document_type" : "list",
"documents" : [
{
"adminc_id" : "5d834741f1e79df70494d614",
"archived" : false,
"company_code" : "AAAAAA",
"created_at" : "2020-09-16T13:07:54.482000+00:00",
"customer_label" : "pomme",
"customer_ref" : "fruit",
"delivered_at" : "2018-11-07T00:00:00+00:00",
"document_type" : "service",
"state" : "delivered",
"subscribed_at" : "2018-11-07T00:00:00+00:00",
"tag" : "EX46V4YQ",
"techc_id" : "5d834741f1e79df70494d614",
"type" : "energy",
"updated_at" : "2020-09-16T13:07:54.482000+00:00"
},
{
"adminc_id" : "5f6e162958becabad3b64047",
"archived" : false,
"company_code" : "BBBBBB",
"created_at" : "2020-09-25T16:18:05.807000+00:00",
"document_type" : "service",
"state" : "subscribed",
"tag" : "T94893JQ",
"techc_id" : "5f6e162958becabad3b64047",
"type" : "telephony",
"updated_at" : "2020-09-25T16:18:05.807000+00:00"
},
{
"adminc_id" : "5f62071fa6dbcad5bf20d832",
"archived" : false,
"company_code" : "AAAAAA",
"created_at" : "2020-09-25T16:15:27.590000+00:00",
"document_type" : "service",
"state" : "subscribed",
"tag" : "TV47X3XQ",
"techc_id" : "5f62071fa6dbcad5bf20d832",
"type" : "telephony",
"updated_at" : "2020-09-25T16:15:27.590000+00:00"
}
],
"limit" : 50,
"offset" : 0
}
I am trying to return the dictionary documents
with the keys value "tag" and "company_code". I tried to extract the dictionary document with a dict.get('document') but the result is returned with the square bracket which does not seems to be a dictionary format :
[
{
"adminc_id" : "5d834741f1e79df70494d614",
"archived" : false,
"company_code" : "AAAAAA",
"created_at" : "2020-09-16T13:07:54.482000+00:00",
"customer_label" : "pomme",
"customer_ref" : "fruit",
"delivered_at" : "2018-11-07T00:00:00+00:00",
"document_type" : "service",
"state" : "delivered",
"subscribed_at" : "2018-11-07T00:00:00+00:00",
"tag" : "EX46V4YQ",
"techc_id" : "5d834741f1e79df70494d614",
"type" : "energy",
"updated_at" : "2020-09-16T13:07:54.482000+00:00"
},
{
"adminc_id" : "5f6e162958becabad3b64047",
"archived" : false,
"company_code" : "BBBBBB",
"created_at" : "2020-09-25T16:18:05.807000+00:00",
"document_type" : "service",
"state" : "subscribed",
"tag" : "T94893JQ",
"techc_id" : "5f6e162958becabad3b64047",
"type" : "telephony",
"updated_at" : "2020-09-25T16:18:05.807000+00:00"
},
{
"adminc_id" : "5f62071fa6dbcad5bf20d832",
"archived" : false,
"company_code" : "AAAAAA",
"created_at" : "2020-09-25T16:15:27.590000+00:00",
"document_type" : "service",
"state" : "subscribed",
"tag" : "TV47X3XQ",
"techc_id" : "5f62071fa6dbcad5bf20d832",
"type" : "telephony",
"updated_at" : "2020-09-25T16:15:27.590000+00:00"
}
]
I don't know how to manage a square bracket format, I need to get this result at the end :
{
"company_code" : "AAAAAA",
"tag" : "EX46V4YQ"
},
{
"company_code" : "BBBBBB",
"tag" : "T94893JQ"
},
{
"company_code" : "AAAAAA",
"tag" : "TV47X3XQ",
}
Can anyone kindly help me understand how it works ? Thank you.
Best regards.
Dictionary in python is one of the most important things you really need to get in touch with in terms of Data Structure in Python
If you want to extract specific value from sub-dictionary, you can use the term of 2-d boundary .
For instance,
books = {"recipes": {"blt": ["bacon", "lettuce", "tomato", "bread"],
"beans_on_toast": ["beans", "bread"],
"scrambles eggs": ["eggs", "butter", "milk"],
"soup": ["tin of soap"],
"pasta": ["pasta", "cheese"]},
"maintenance": {"stuck": ["oil"],
"loose": ["gaffer tape"]}}
print(books["recipes"]["soup"])
print(books["recipes"]["scrambles eggs"])
print(books["maintenance"]["loose"])
This book
Dictionary is complex disc
. If you want to access sub-key of one of its values, you will need to use the term of 2-d boundary .
After debugging This'll be the result:
['tin of soap']
['eggs', 'butter', 'milk']
['gaffer tape']
You can also check these Github links for more features and examples about the dictionary and data structure overall in python;
Solution:
This code snippet might solve your issue:
in_dict = {
"count" : 3,
"document_type" : "list",
"documents" : [
{
"adminc_id" : "5d834741f1e79df70494d614",
"archived" : False,
"company_code" : "AAAAAA",
"created_at" : "2020-09-16T13:07:54.482000+00:00",
"customer_label" : "pomme",
"customer_ref" : "fruit",
"delivered_at" : "2018-11-07T00:00:00+00:00",
"document_type" : "service",
"state" : "delivered",
"subscribed_at" : "2018-11-07T00:00:00+00:00",
"tag" : "EX46V4YQ",
"techc_id" : "5d834741f1e79df70494d614",
"type" : "energy",
"updated_at" : "2020-09-16T13:07:54.482000+00:00"
},
{
"adminc_id" : "5f6e162958becabad3b64047",
"archived" : False,
"company_code" : "BBBBBB",
"created_at" : "2020-09-25T16:18:05.807000+00:00",
"document_type" : "service",
"state" : "subscribed",
"tag" : "T94893JQ",
"techc_id" : "5f6e162958becabad3b64047",
"type" : "telephony",
"updated_at" : "2020-09-25T16:18:05.807000+00:00"
},
{
"adminc_id" : "5f62071fa6dbcad5bf20d832",
"archived" : False,
"company_code" : "AAAAAA",
"created_at" : "2020-09-25T16:15:27.590000+00:00",
"document_type" : "service",
"state" : "subscribed",
"tag" : "TV47X3XQ",
"techc_id" : "5f62071fa6dbcad5bf20d832",
"type" : "telephony",
"updated_at" : "2020-09-25T16:15:27.590000+00:00"
}
],
"limit" : 50,
"offset" : 0
}
This loop will iterate over the dictionaries that are present inside the list, as shown in the question:
for inner_dict in in_dict['documents']:
print(inner_dict)*
OUTPUT:
{'adminc_id': '5d834741f1e79df70494d614', 'archived': False, 'company_code': 'AAAAAA', 'created_at': '2020-09-16T13:07:54.482000+00:00', 'customer_label': 'pomme', 'customer_ref': 'fruit', 'delivered_at': '2018-11-07T00:00:00+00:00', 'document_type': 'service', 'state': 'delivered', 'subscribed_at': '2018-11-07T00:00:00+00:00', 'tag': 'EX46V4YQ', 'techc_id': '5d834741f1e79df70494d614', 'type': 'energy', 'updated_at': '2020-09-16T13:07:54.482000+00:00'}
{'adminc_id': '5f6e162958becabad3b64047', 'archived': False, 'company_code': 'BBBBBB', 'created_at': '2020-09-25T16:18:05.807000+00:00', 'document_type': 'service', 'state': 'subscribed', 'tag': 'T94893JQ', 'techc_id': '5f6e162958becabad3b64047', 'type': 'telephony', 'updated_at': '2020-09-25T16:18:05.807000+00:00'}
{'adminc_id': '5f62071fa6dbcad5bf20d832', 'archived': False, 'company_code': 'AAAAAA', 'created_at': '2020-09-25T16:15:27.590000+00:00', 'document_type': 'service', 'state': 'subscribed', 'tag': 'TV47X3XQ', 'techc_id': '5f62071fa6dbcad5bf20d832', 'type': 'telephony', 'updated_at': '2020-09-25T16:15:27.590000+00:00'}
**NOTE: You can select the required keys using inner_dict[KEY_NAME]
**
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.