How do I get the gender value?
The datetime key might be different, so I want to be able to access it no matter what the key name there is.
{'data': {'2021-01-20 01:14:47 UTC': {'gender': 2, 'goodHost': 0, }}}
How do I get the gender value?
The datetime key might be different, so I want to be able to access it no matter what the key name there is.
{'data': {'2021-01-20 01:14:47 UTC': {'gender': 2, 'goodHost': 0, }}}
If you can guarantee that there will only be one datetime key ever, you could do something like:
data = my_dict['data']
item = list(data.values())[0]
print(item['gender'])
Basically, my_dict['data'].values()
will return all of the values in the nested dictionary without caring what the key is. If you know there will only be one such item, you can go directly to it that way.
If you might have multiple keys in there, you can loop across the values and look for the ones that have the gender
key:
for value in my_dict["data"].values():
try:
print(value["gender"])
except KeyError:
print("Didn't have a gender key so I'm skipping it.")
Just take the first=only value.
>>> next(iter(d['data'].values()))['gender']
2