access a dict inside of a dict using strings? [duplicate] - python

This question already has answers here:
Access nested dictionary items via a list of keys?
(20 answers)
Closed 7 months ago.
I need to make a method that lets you navigate dicts of various depths using a simple string.
How can I make get_from_dict(dict, "images.player.left") work? thaks

Split the string, and then look up the keys in a loop.
>>> def get_from_dict(d, keys):
... for key in keys.split("."):
... d = d[key]
... return d
...
>>> get_from_dict({"images": {"player": {"left": "tada!"}}}, "images.player.left")
'tada!'

Related

Search dictionary values for an element [duplicate]

This question already has answers here:
Convert a python dict to a string and back
(12 answers)
Closed 3 years ago.
I have a predefined dictionary, and I want to be able to search the keys and values of the dictionary for a target string.
For example, if this is my dictionary:
my_dict = {u'GroupName': 'yeahyeahyeah', u'GroupId': 'sg-123456'}
I want to be check whether 'yeahyeahyeah' or 'GroupId' are in the keys or values of the dictionary. I think I want to convert the entire dictionary into a string, so that I can just do a substring search, but am open to other ideas.
EDIT: taking advice from comments
Here's how to create a list from the key value pairs in your dictionary.
my_dict = [{u'GroupName': 'yeahyeahyeah', u'GroupId': 'sg-123456'}]
my_list = list(my_dict[0].keys()) + list(my_dict[0].values())
This returns:
['GroupName', 'yeahyeahyeah', 'GroupId', 'sg-123456']

Iterate through a json list with dictionaries [duplicate]

This question already has an answer here:
How to loop over a list of dicts and print values of a specific key?
(1 answer)
Closed 4 years ago.
I have a json file which contains a list with many dictionaries. Here I've included just two. But I want to iterate through all of the dictionaries and only get the code value (Company1 and Company2).
[{"code":"Company1","exchange_short_name":"ST","date":"2000-01-01"},
{"code":"Company2","exchange_short_name":"ST","date":"2000-01-01"}]
I've gotten to this, but it gives me all of the values inside the dictionary, and not the code values.
for d in jsonData:
for key in d:
print(d[key])
[item['code'] for item in jsonData]
will return list of codes.

How to get keys from the dictionary? [duplicate]

This question already has answers here:
How to return dictionary keys as a list in Python?
(13 answers)
Closed 5 years ago.
list=['Mary','Bob','Linda']
dictionary={0:'Mary', 1: 'Anna', 2:'Bob', 3:'Alice', 4: 'Linda'}
if list in set(dictionary.values()):
name = dictionary.get(None, list)
values = itemgetter(!*dic)(dictionary)
How can I get keys from the dictionary as the code find out same values from the list and dictionary?
Second question: how can I get different values keys of the dictionary between a list and a dictionary? E,g: print out the keys are [1],[3]
Thank you
I'm surprised you didn't try the obvious:
dictionary.keys()
Note: this was provided when the question explicitly asked "How to get all keys from the dictionary?".

Merge two lists,one as keys, one as values, into a dict in Python [duplicate]

This question already has answers here:
How do I combine two lists into a dictionary in Python? [duplicate]
(6 answers)
Closed 7 years ago.
Is there any build-in function in Python that merges two lists into a dict? Like:
combined_dict = {}
keys = ["key1","key2","key3"]
values = ["val1","val2","val3"]
for k,v in zip(keys,values):
combined_dict[k] = v
Where:
keys acts as the list that contains the keys.
values acts as the list that contains the values
There is a function called array_combine that achieves this effect.
Seems like this should work, though I guess it's not one single function:
dict(zip(["key1","key2","key3"], ["val1","val2","val3"]))
from here: How do I combine two lists into a dictionary in Python?

initialize dict with keys,values from two list [duplicate]

This question already has answers here:
How can I make a dictionary (dict) from separate lists of keys and values?
(21 answers)
Closed 4 years ago.
I have read this link
But how do I initialize the dictionary as well ?
say two list
keys = ['a','b','c','d']
values = [1,2,3,4]
dict = {}
I want initialize dict with keys & values
d = dict(zip(keys, values))
(Please don't call your dict dict, that's a built-in name.)
In Python 2.7 and python 3, you can also make a dictionary comprehension
d = {key:value for key, value in zip(keys, values)}
Although a little verbose, I found it more clear, as you clearly see the relationship.

Categories