How to access key from python dict? [duplicate] - python

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

Related

Why Python Dict is not printing the same order that inserted? [duplicate]

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)

Can a list be used as a key in a dictionary in python? [duplicate]

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'

Is there an easy way to use a nested dictionary entry and ensure none of the keys are missing? [duplicate]

This question already has answers here:
Elegant way to check if a nested key exists in a dict?
(18 answers)
Closed 3 years ago.
I want to do the following with less code:
if "address" in structured_data:
if "addressCountry" in structured_data["address"]:
data["country"] = structured_data["address"]["addressCountry"]
Is this possible?
What about using a try?
try:
data["country"] = structured_data['address']['addressCountry']
except KeyError:
pass # handle what you want to do

Duplicate Key in dictionary in python [duplicate]

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?

Showing Syntax Error while try to output the dictionary or a list value [duplicate]

This question already has answers here:
Syntax error on print with Python 3 [duplicate]
(3 answers)
Closed 9 years ago.
I am a amature in Python,using the python33 version.The problem i am facing while i want to get output of a list of dictionary just like that.
dict = {'name':'Tanvir','Position':'Programmer'};
print dict['name'];
if i run the code then there showing a syntax error.same thing is happening for the list also.
Please help me to fix the problem.
Thanks in advance.
In Python 3, print is a function, so you need print(dict[name]). You also don't need the semicolons. You also need to read the Python tutorial to learn the basics first.

Categories

Resources