-1

Here is my list of dictionary.

d = [{'count': '100', 'zone': 'PIMPRI', 'cycle': '15'},
     {'count': '50', 'zone': 'PIMPRI', 'cycle': '15'},
     {'count': '150', 'zone': 'PIMPRI', 'cycle': '30'}]

How can I get output like below:

d_one = [{'count': '100', 'zone': 'PIMPRI', 'cycle': '15'},
         {'count': '50', 'zone': 'PIMPRI', 'cycle': '15'}]

d_two = [{'count': '150', 'zone': 'PIMPRI', 'cycle': '30'}]

Getting distinct data from list of dictionary from two keys zone and cycle. For same zone and cycle pair create one list of dictionary and it goes till end.

Aran-Fey
  • 39,665
  • 11
  • 104
  • 149
Luffy
  • 83
  • 1
  • 10
  • What have you tried, and what exactly is the problem with it? – jonrsharpe May 28 '18 at 09:43
  • I am using django framework. So by using its ORM it gives me list of dictionary which I have given in d variable. So I will get variable data and list items too. Sometimes I may have 1 item in it or 50 too. So I want create new dictionary of same values of zone and cycle key. If total pair is 3 or 5 then dictionary should get created respectiely – Luffy May 28 '18 at 09:53
  • That doesn't answer my question at all: where's your attempt to implement this functionality? SO isn't a code-writing service. – jonrsharpe May 28 '18 at 09:54
  • My dear friend, of course I tried something which was not working. I tried something as doing looping and then comparing but the problem was that I was not on the right way as I had no clue what to do with this. That's why I called for help. Hope you understand. Thanks – Luffy Aug 14 '18 at 13:56
  • So *where is it*? Give a [mcve]. – jonrsharpe Aug 14 '18 at 14:04

5 Answers5

1

Make a dict using pairs (dct['zone'], dct['cycle']) as keys:

>>> result = {}
>>> 
>>> for dct in d:
...     result.setdefault((dct['zone'], dct['cycle']), []).append(dct)
... 
>>> result
{('PIMPRI', '15'): [{'count': '100', 'zone': 'PIMPRI', 'cycle': '15'}, {'count': '50', 'zone': 'PIMPRI', 'cycle': '15'}], ('PIMPRI', '30'): [{'count': '150', 'zone': 'PIMPRI', 'cycle': '30'}]}
Paul Panzer
  • 51,835
  • 3
  • 54
  • 99
  • I modified code a bit and done with this approach. Thanks all and sorry for late comment. – Luffy Aug 14 '18 at 13:53
0

You want to filter your data :

d_one = filter(lambda d:d['cycle']=='15',d)
> [{'count': '100', 'zone': 'PIMPRI', 'cycle': '15'}, 
>  {'count': '50', 'zone': 'PIMPRI', 'cycle': '15'}]

and

d_two = filter(lambda d:d['cycle']=='30',d)
> [{'count': '150', 'zone': 'PIMPRI', 'cycle': '30'}]
Hearner
  • 2,711
  • 3
  • 17
  • 34
0

One approach is to use collections. And have your variable('cycle') as key in a dict.

Ex:

import collections 
res = collections.defaultdict(list)
d = [{'count': '100', 'zone': 'PIMPRI', 'cycle': '15'},
     {'count': '50', 'zone': 'PIMPRI', 'cycle': '15'},
     {'count': '150', 'zone': 'PIMPRI', 'cycle': '30'}]

for i in d:
    res[i["cycle"]].append(i)
print(res['15'])
print("-----")
print(res['30'])

Output:

[{'count': '100', 'zone': 'PIMPRI', 'cycle': '15'}, {'count': '50', 'zone': 'PIMPRI', 'cycle': '15'}]
-----
[{'count': '150', 'zone': 'PIMPRI', 'cycle': '30'}]
Rakesh
  • 81,458
  • 17
  • 76
  • 113
0

Try with groupby,

from itertools import groupby 
from operator import itemgetter
grouper = itemgetter("cycle", "zone")
dict_one,dict_two = (list(g) for _,g in groupby(d,key=grouper))

The code will return a tuple now we have only 2 elements. So the value assignment will work here. If you have more values you can assign these values to a dict or store as iterable.

Output

In [9]: dict_one
Out[9]: 
[{'count': '100', 'cycle': '15', 'zone': 'PIMPRI'},
 {'count': '50', 'cycle': '15', 'zone': 'PIMPRI'}]

In [10]: dict_two
Out[10]: [{'count': '150', 'cycle': '30', 'zone': 'PIMPRI'}]
Rahul K P
  • 15,740
  • 4
  • 35
  • 52
0

Why to use separate dictionaries for all those values. Couldn't you make it to a single dictionary. For your example,

d = [{'count': '100', 'zone': 'PIMPRI', 'cycle': '15'},
     {'count': '50', 'zone': 'PIMPRI', 'cycle': '15'},
     {'count': '150', 'zone': 'PIMPRI', 'cycle': '30'}]

this is what we could get,

res = {}
for ele in d:
    res.setdefault((ele['cycle'],ele['zone']), [])
    res[(ele['cycle'], ele['zone'])].append(ele['count'])
ravi
  • 10,994
  • 1
  • 18
  • 36