Skip to content

Instantly share code, notes, and snippets.

@pmbaumgartner
Created February 21, 2023 20:27
Show Gist options
  • Save pmbaumgartner/955aa8100c37e3cbb4bbd9649954b0cd to your computer and use it in GitHub Desktop.
Save pmbaumgartner/955aa8100c37e3cbb4bbd9649954b0cd to your computer and use it in GitHub Desktop.
create a dictionary based off of string variable names. based on rust struct shorthand init.
def ndict(*args):
"""Return a dictionary with the given keys and their values from globals."""
g = globals()
result = {}
for arg in args:
if arg in g:
result[arg] = g[arg]
else:
raise KeyError(f"Key '{arg}' not found in globals")
return result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment