Just now I had to insert a certain dictionary variable into a database, and i wanted to use sqlite’s named placeholder scheme to make things simple.
self.cur.executemany('insert into mytable(id, name, field1, field2) values(:id, :name, :foo, :bar)',data)
Now the problem was that this dict called data didn’t have keys “foo” and “bar”: {'id': 1, 'name': u'name', 'keys': [1, 2]}.
I’d need to map this into the shape of {'id': 1, 'name': u'name', 'foo':1, 'bar':2} to use in the database query. Also, i wanted to keep the whole thing in a single command for reasons unknown. In the end, I came up with the following: map(lambda y: y.update({'foo':y['keys'][0], 'bar':y.pop(u'keys')[1]}) or y, data)
This did the trick. What it does: updates y with the ‘foo’ key as needed, then adds the ‘bar’ key as well, while popping off the unnecessary ‘keys’ key. The “or y” needs to be there, because dict.update returns None, not the dict object.