1

Python: 3.7.3

I have a string which needs to be converted to dictionary.

So, I have achieved it using ast.literal_eval method.

my_string = "{'ReceiptMessageId':'foo','ReceiptMessageId':'boo','ReceiptMessageName':'zoo'}"

import ast
my_dict = ast.literal_eval(my_string)

But issue here is, string (my_string) has same key with different values and converted dictionary replaces with lastly received value.

Expected:

{'ReceiptMessageId': ['foo','boo'], 'ReceiptMessageName': 'zoo'}

Actual:

{'ReceiptMessageId': 'boo', 'ReceiptMessageName': 'zoo'}

After googling a lot, I found this can be achieved using defaultdict from collections but in my case, duplicates are already ignored while converting into dict. Can someone give me an idea as to how to go about it?

Learner
  • 481
  • 1
  • 15
  • 28

0 Answers0