I'm getting a weird key error with Python dicts. My key is "B19013_001E" and I've named my dict "sf_tracts" with a nested dict "properties". Here is my code:
x = "B19013_001E"
for tract in sf_tracts:
print tract["properties"][x]
With this, I get a KeyError: "B19013_001E"
However if I change the code to this, the values get printed:
x = "B19013_001E"
for tract in sf_tracts:
for key in tract["properties"]:
if key == "B19013_001E":
print tract["properties"][x]
What's the difference?
-edit- I believe the issue is the underscore as other keys can be printed. How do I access this key?
Thanks
sf_tractsprint list(tract['properties'])produce? Can you share the exact quoted representation of that one key?sf_tracts. Not all those dictionaries have that key. Yourfor key in dictionaryloop can be replaced withif x in tract["properties"]: print tract["properties"][x], by the way, no need to loop.