Dictionary KeyNotFound

Quick question for someone more knowledgeable than I - why does the first line of code work but the second doesn't? It seems to me I'm doing the same thing two equivalent ways, yet the {...} method throws an error while dict([]) method doesn't:

print dict([(key,locals()[key]) for key in ["Type","NoPoints","PointNos","Zoff"]])

print       {key:locals()[key] for key in ["Type","NoPoints","PointNos","Zoff"]}


{'Zoff': ['1', '1', '0', '0'], 'NoPoints': 4, 'Type': 'PANEL', 'PointNos': ['253', '254', '254', '253']}

Runtime error (KeyNotFoundException): Type

    line 143, in AddArea, "<string>"

#EDIT - I corrected the key,locals() to key:locals() (which is what I have in my editor - apologies for the bad copy/paste job I did there!) My error still stands though

I should also add that I'm using Python 2.7 (IronPython)

Read more here: https://stackoverflow.com/questions/68482945/dictionary-keynotfound

Content Attribution

This content was originally published by Martial_Marlon at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: