KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
In C++ often do something like this: typedef map<int, vector<int> > MyIndexType; Where I then use it like this: MyIndexType myIndex; for( ... some loop ...) { myIndex[someId].push_back(someVal); } If there was no entry in the map the code will insert a new empty vector and then append to it. In Python it would look like this: myIndex = {} for (someId,someVal) in collection: try: myIndex[someId].append(someVal) except KeyError: myIndex[someId] = [someVal] The try except is a bit ugly here. Is there a way to tell the dictionary an object type to insert when a KeyError is encountered at dictionary declaration time?
Tags (comma-separated)
Save Edits
Cancel