How to access the list inside dictionary in python? - python

I have dict structure as shown in image.I am want to access wavelength list values of it.
How to access it or store the wavelength list in another variable.

This should simply be:
dict['wavelength']
Where "dict" is your dictionary name; which looks like it's "n2" maybe. The returned value will be the list that you're looking for.

Related

Python Search Dictionary by Key to return Value

I have a fairly complex dictionary with lists that I'm trying to use. I'm attempting to search the dictionary for the key "rows", which has a list.
With my current sample of data, I can easily pull it like so with index operator:
my_rows = my_dict['inputContent']['document']['fields'][2]['value']['rows']
'rows' is a list and those are the values I am trying to pull. However, my data from 'rows' won't always be in the exact same location but it will always be in my_dict. It could be in a different index like so:
my_dict['inputContent']['document']['fields'][4]['value']['rows']
or
my_dict['inputContent']['document']['fields'][7]['value']['rows']
Really any number.
I've tried using just the basic:
my_rows = my_dict.get("rows")
But this returns None.
I find lots of articles on how to search for values and return key, but I know the key and it will always be the same, while my values in 'rows' will always be different.
I'm new to python and using dictionaries in general, but i'm really struggling to drill down into this dictionary to pull this list.
my_dict['inputContent']['document']['fields'][2]['value']['rows']
my_dict['inputContent']['document']['fields'][4]['value']['rows']
my_dict['inputContent']['document']['fields'][7]['value']['rows']
Looks like the overall structure is the same, the only variable is the numeric list index of the fourth element. So we need a loop that iterates over each element of that list:
for element in my_dict['inputContent']['document']['fields']:
if 'rows' in element['value']:
# found it!
print(element['value']['rows'])

Do elements of a list inside a dictionary change it's order in Python?

I have a list inside a dictionary, it seems to change it's order in every iteration.
Consider this example,
a={ID: [value, [1,2,3,4,5,6]]}
When I access a[ID][1], will it give [1,2,3,4,5,6] (in order) everytime I access this?
Yes, a[ID][1] should give the same list in order each time. Unless you change a somewhere else within your program.

indexing nested dictionary elements

I am trying to access the dictionary elements in python, which looks like this:
mydict= {'message':'','result':[{'abc':1,'xyz':2}]}
I want to access the value of key 'xyz'.Is there any direct method to access it in python.
Any help is greatly appreciated.
In steps,
mydict['result'] returns [{'abc':1,'xyz':2}]
mydict['result'][0] returns {'abc':1,'xyz':2}
mydict['result'][0]['xyz'] returns 2
The answer above is wrong in that mydict['result'][0] doesn't work because you don't index dictionaries like a list.
To access 1, you must use the actual string in the key of the nested dictionary: mydict['result']['abc']

Writing to a specific dictionary inside a json file Python

I have a .json file with some information
{"items": [{"phone": "testp"}, {"phone": "Test2"}]}
I want to add another dictionary into the next available slot in the array with some more information. I have tried many ways however no of them work, has anyone got any ideas?
dictionary["items"].append(new_dictionary)
You access key "items" in your dictionary, which is a list of dictionaries, to that list you simply append your new dictionary
You can read the json into a variable using load function.
And then append the value of array using
myVar["items"].append(dictVar)

How to set value in python dictionary while looking it up from another dictionary?

I have two dictionaries. The first is mapping_dictionary, it has several key-value pairs. It will serve as a reference. The second dictionary only has two key-value pairs. I would like to look up the value that should be assigned to the second dictionary in the mapping_dictionary and set it to one of the values. I tried doing it a few different ways but no success.
Please let me know if the syntax is wrong or if this is not the way to do something like this in Python? Thank you in advance for any help.
Example 1:
mapping_dictionary={'TK_VAR_DEC':1, 'TK_ID':2, 'TK_COMMA':3}
token_dictionary={'TK_TYPE', 'TK_VALUE'}
tk_v=mapping_dictionary.get("TK_VAR_DEC")
token_dictionary['TK_TYPE']=tk_v
token_dictionary['TK_VALUE']="VAR_DEC"
Example 2:
token_dictionary['TK_TYPE']=mapping_dictionary.get("TK_VAR_DEC")
token_dictionary['TK_VALUE']="VAR_DEC"
With the definition of the token_dictionary, you're not defining a dictionary at all -- you've written the literal syntax for a set. You need to specify values for it to be a dictionary. I expect that if you change to using token_dictionary = {'TK_TYPE': None, 'TK_VALUE': None} you'll have more luck.
Also note that using .get() is unnecessary for retrieving a value from the dictionary. Just use [].

Categories