This question already has answers here:
How can one make a dictionary with duplicate keys in Python?
(9 answers)
Python JSON parse duplicate records
(1 answer)
Closed 3 years ago.
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?
Related
This question already has answers here:
Are dictionaries ordered in Python 3.6+?
(6 answers)
Closed 11 months ago.
I am using the following code to print keys and values of a dict.
for key, val in index1.items():
print("++++++++")
print ( key)
print (val)
print("++++++++")
I want it's to be printed in the same order as it was inserted. Currently, it's not following that.
It is not printing in the same order because the order is not important since you can't get any values with indexes because you use strings as keys to get values. But you can always use .sorted() if you want (it doesn't impact anything in the dictionary, just the printing part)
This question already has answers here:
Why can't I use a list as a dict key in python?
(11 answers)
Closed last year.
I read that list cannot be used as a key in a dictionary in python but I tried and its working fine. What am I missing? This is what I tried.
a={'hello':2,'[2,4,3]':'hi'}
print(a)
gives:
{'hello': 2, '[2,4,3]': 'hi'}
'[2,4,3]' is a string. If you take away the quotation marks, then it's a list. And if you try to assign it as a key you'd get:
TypeError: unhashable type: 'list'
This question already has answers here:
How to convert string representation of list to a list
(19 answers)
Closed 3 years ago.
I have a form that save lists as strings, I want to convert those strings to lists before save in DB.
I'm using python3 and Django.
The strings looks like:
'["MEX","MTY","GDL","BJX","QRO","PBC","TLC","CUN","EM","CUU"]'
what will be the most pythonic way to do that?
import json
json.loads('["MEX","MTY","GDL","BJX","QRO","PBC","TLC","CUN","EM","CUU"]')
This question already has answers here:
Removing u in list
(8 answers)
Closed 3 years ago.
I have a list of id's and I am trying the following below:
final = "ids: {}".format(tuple(id_list))
For some reason I am getting the following:
"ids: (u'213231231', u'weqewqqwe')
Could anyone help out on why the u is coming inside my final string. When I am trying the same in another environment, I get the output without the u''. Any specific reason for this?
Actually it is unicode strings in python
for literal value of string you can fist map with str
>>> final = "ids: {}".format(tuple(map(str, id_list)))
>>> final
"ids: ('213231231', 'weqewqqwe')
This question already has answers here:
How can I get dictionary key as variable directly in Python (not by searching from value)?
(15 answers)
Get key by value in dictionary
(43 answers)
Closed 5 years ago.
How to access key 'Description' from dict below?
{text:u'Description': u'ABC',text:u'Balance': u'35,402,678.51',text:u'Credit': u'10,000.00'}
Tried using mydict.get('Description') & mydict['Description']. please provide your suggestions, i am new to python.
“text:” is redundant, it can be left out.
dic = {u'Description': u'ABC', u'Balance': u'35,402,678.51', u'Credit': u'10,000.00'}
In your example, all your keys have a strange text: prefix, that result in an invalid dictionary (or rather in invalid syntax).
Otherwise your attempts are correct:
mydict = {u'Description': u'ABC',
u'Balance': u'35,402,678.51',
u'Credit': u'10,000.00'}
v = mydict.get('Description')
print(v) # ABC
By the way, if you get an error, you should add the complete traceback to your question, this will make it easier for us to help you.
Got the solution. error was due to "text" prefix. eliminated it using (myxlrdcell).value