'Python not evaluating statement, just printing
I have a dictionary which holds multiple entries.
e.g. I have response[0]['values']['id'] and response[2]['values']['id'].
response[0] has no key 'id' whereas response[2] does.
I'm looping through the responses but everytime I try and evaluate
response[0]['values']['id'] it just prints 'id'. Even if I try if (response[0]['values']['id']) or type(response[0]['values']['id']), it does not evaluate the statement but just prints 'id'.
response = requests.get (......get command)
test = type((response[0]['value']['id']))
print(test) # 'id'
The statement is not being evaluated and it don't give me the value.
Anyone know why?
Thanks!
Solution 1:[1]
maybe a different conditional statement like searching through the dict keys will work
if ('id' in response[0]['values'].keys()): print(response[0]['values']['id'])
Solution 2:[2]
Fixed it by adding except Keyerror!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Steve |
| Solution 2 | ryandanielsim |
