Skip to content Skip to sidebar Skip to footer

Tuple Of Tuple Of Dict From Mysql Database

I am running MySQL query from python that returning tuple of dict, lets call it result. Now each dict has 3 key/value pairs as an element, one of these 3 element is date. Now I wan

Solution 1:

One good looking solution would be to store them inside a dictionary:

>>>t = ({"a":2}, {"a":2}, {"a":3})>>>import collections>>>d = collections.defaultdict(list)>>>for i in t:...    d[i['a']].append(i)...

Now, this is obviously not what you want but this is better than creating the list of lists inside a loop directly in terms of speed, also a dictionary seems to be a better fit for this kind of data. This can also be converted to whatever you want easily:

>>> [k for c,k in d.items()][[{'a': 2}, {'a': 2}], [{'a': 3}]]

If the speed is critical, you can sort the db results by date, in which case you can get a better algorithm.

Post a Comment for "Tuple Of Tuple Of Dict From Mysql Database"