Skip to content

Instantly share code, notes, and snippets.

@Tarliton
Created September 26, 2019 03:17
Show Gist options
  • Save Tarliton/d92ffe6ecfd3a7975f74b0d10074349b to your computer and use it in GitHub Desktop.
Save Tarliton/d92ffe6ecfd3a7975f74b0d10074349b to your computer and use it in GitHub Desktop.
from collections import defaultdict
create_dict = lambda: defaultdict(create_dict)
def auto_defaultidct(keys, d=create_dict()):
buffer = ''
for i, item in enumerate(range(len(keys))):
if i == 0:
buffer = 'd'
buffer += f'[keys[{item}]]'
buffer += ' = create_dict()'
exec(buffer, None, locals())
return d
auto_defaultidct('this function call will create a defaultdict that expands for every word in this sentece'.split(' '))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment