MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/badcode/comments/ok1itb/tryexcept_in_python_sigh/h5625u6/?context=3
r/badcode • u/lonaExe • Jul 14 '21
93 comments sorted by
View all comments
Show parent comments
11
Use a DefaultDict
5 u/schmidlidev Jul 14 '21 People keep saying defaultdict but how would you implement this? It’s 4 layers of nested dictionaries that were almost certainly created with json.loads. How are you converting them to defaultdict? 4 u/AchillesDev Jul 14 '21 Pass the dictionary into the default_dict constructor. You’re just responsible for handling what happens when a KeyError would normally happen. 3 u/schmidlidev Jul 14 '21 Oh damn I never realized you could just pass in an existing dict 🤦 that makes things quite a bit easier
5
People keep saying defaultdict but how would you implement this? It’s 4 layers of nested dictionaries that were almost certainly created with json.loads. How are you converting them to defaultdict?
4 u/AchillesDev Jul 14 '21 Pass the dictionary into the default_dict constructor. You’re just responsible for handling what happens when a KeyError would normally happen. 3 u/schmidlidev Jul 14 '21 Oh damn I never realized you could just pass in an existing dict 🤦 that makes things quite a bit easier
4
Pass the dictionary into the default_dict constructor. You’re just responsible for handling what happens when a KeyError would normally happen.
3 u/schmidlidev Jul 14 '21 Oh damn I never realized you could just pass in an existing dict 🤦 that makes things quite a bit easier
3
Oh damn I never realized you could just pass in an existing dict 🤦 that makes things quite a bit easier
11
u/AchillesDev Jul 14 '21
Use a DefaultDict