By Hari


2019-03-14 20:10:27 8 Comments

I have the following dictionary and I would like to append one more array to 'CorrectionsAll'. I tried Append option but I couldn't get what I needed. Could someone please help me with the logic.

Dict1 = {'ShpmtID': 123, 'PickupDate': '2016/01/01 00:00:00', 'EstimatedDeliveryDate': '2016/01/10 00:00:00', 'OrigSic': 'LJB', 'DestSic': 'XCF', 'CorrectionHistory': [{'key': 405013, 'CorrectionsAll': [{'CorrChngDesc': 'Commodity Line Changed'}]}]}

Wanted to add "Discount Line changed" as follows:

{'ShpmtID': 123, 'PickupDate': '2016/01/01 00:00:00', 'EstimatedDeliveryDate': '2016/01/10 00:00:00', 'OrigSic': 'LJB', 'DestSic': 'XCF', 'CorrectionHistory': [{'key': 405013, 'CorrectionsAll': [{'CorrChngDesc': 'Commodity Line Changed'}, {'CorrChngDesc': 'Discount Line Changed'}]}]}

Copy Comment: tried these two options:

Option-1:

Dict1["CorrectionHistory"]["CorrectionsAll"].append({'CorrChngDesc': 'Discount Line Changed'})  

Option-2:

Dict1["CorrectionsAll"].append({'CorrChngDesc': 'Discount Line Changed'})

2 comments

@Fukiyel 2019-03-14 20:26:50

I tried these two options: Option-1: Dict1["CorrectionHistory"]["CorrectionsAll"].append({'CorrChngDesc': 'Discount Line Changed'})

Option-1 won't work because "CorrectionAll" is not an element of "CorrectionHistory", but of a list. That list, IS an element of it though.

Dict1["CorrectionHistory"][0]["CorrectionsAll"] # Use [0] to go inside this list !

Option-2: Dict1["CorrectionsAll"].append({'CorrChngDesc': 'Discount Line Changed'})

Option-2 won't work either, because Dict1["CorrectionAll"] doesn't exist. It's again, not a direct element of it, but I think you can understand why now.

@Hari 2019-03-14 20:34:42

Thank you @Fukiyel for your explanation. I understand it much better now.

@Reedinationer 2019-03-14 20:12:13

You just access the list you want and then append to that

Dict1['CorrectionHistory'][0]['CorrectionsAll'].append({'CorrChngDesc': 'Discount Line Changed'})

Outputs:

{'OrigSic': 'LJB', 'PickupDate': '2016/01/01 00:00:00', 'DestSic': 'XCF', 'ShpmtID': 123, 'CorrectionHistory': [{'key': 405013, 'CorrectionsAll': [{'CorrChngDesc': 'Commodity Line Changed'}, {'CorrChngDesc': 'Discount Line Changed'}]}], 'EstimatedDeliveryDate': '2016/01/10 00:00:00'}

@mad_ 2019-03-14 20:13:03

It will give keyError

@Hari 2019-03-14 20:13:51

Hi Reedinationer, I did try this option but I am getting the following error: KeyError: 'CorrectionsAll'

@Reedinationer 2019-03-14 20:18:41

@mad_ Yeah I made a mistake, should have tested it locally, but was feeling lazy cause the OP didn't even post an attempt

@Reedinationer 2019-03-14 20:18:49

@Hari it will work now

@Reedinationer 2019-03-14 20:19:25

@roganjosh you are correct. I needed to index the dict then the list, then the other dict

@Reedinationer 2019-03-14 20:19:49

@ChristianDean Thanks! I ran it locally and figured that out just as you posted it

@Christian Dean 2019-03-14 20:20:12

No problem! @Reedinationer

@Hari 2019-03-14 20:32:44

Thanks everyone and Thanks Reedinationer! It worked.

Related Questions

Sponsored Content

20 Answered Questions

21 Answered Questions

22 Answered Questions

[SOLVED] Check if a given key already exists in a dictionary

  • 2009-10-21 19:05:09
  • Mohan Gulati
  • 2843363 View
  • 2682 Score
  • 22 Answer
  • Tags:   python dictionary

34 Answered Questions

[SOLVED] How do I sort a dictionary by value?

14 Answered Questions

[SOLVED] Iterating over dictionaries using 'for' loops

15 Answered Questions

[SOLVED] What are metaclasses in Python?

15 Answered Questions

[SOLVED] Add new keys to a dictionary?

39 Answered Questions

[SOLVED] How to merge two dictionaries in a single expression?

56 Answered Questions

[SOLVED] Calling an external command in Python

19 Answered Questions

Sponsored Content