Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can make use of <a href="http://docs.python.org/2/library/itertools.html#itertools.groupby" rel="nofollow">itertools.groupby</a>:</p> <pre><code>from itertools import groupby from operator import itemgetter from pprint import pprint data = [ { 'Organization' : '123 Solar', 'Phone' : '444-444-4444', 'Email' : '', 'website' : 'www.123solar.com' }, { 'Organization' : '123 Solar', 'Phone' : '', 'Email' : 'joey@123solar.com', 'Website' : 'www.123solar.com' }, { 'Organization' : '234 test', 'Phone' : '111', 'Email' : 'a@123solar.com', 'Website' : 'b.123solar.com' }, { 'Organization' : '234 test', 'Phone' : '222', 'Email' : 'ac@123solar.com', 'Website' : 'bd.123solar.com' }] data = sorted(data, key=itemgetter('Organization')) result = {} for key, group in groupby(data, key=itemgetter('Organization')): result[key] = [item for item in group] pprint(result) </code></pre> <p>prints:</p> <pre><code>{'123 Solar': [{'Email': '', 'Organization': '123 Solar', 'Phone': '444-444-4444', 'website': 'www.123solar.com'}, {'Email': 'joey@123solar.com', 'Organization': '123 Solar', 'Phone': '', 'Website': 'www.123solar.com'}], '234 test': [{'Email': 'a@123solar.com', 'Organization': '234 test', 'Phone': '111', 'Website': 'b.123solar.com'}, {'Email': 'ac@123solar.com', 'Organization': '234 test', 'Phone': '222', 'Website': 'bd.123solar.com'}]} </code></pre> <p>UPD:</p> <p>Here's what you can do to group items into single dict:</p> <pre><code>for key, group in groupby(data, key=itemgetter('Organization')): result[key] = {'Phone': [], 'Email': [], 'Website': []} for item in group: result[key]['Phone'].append(item['Phone']) result[key]['Email'].append(item['Email']) result[key]['Website'].append(item['Website']) </code></pre> <p>then, in <code>result</code> you'll have:</p> <pre><code>{'123 Solar': {'Email': ['', 'joey@123solar.com'], 'Phone': ['444-444-4444', ''], 'Website': ['www.123solar.com', 'www.123solar.com']}, '234 test': {'Email': ['a@123solar.com', 'ac@123solar.com'], 'Phone': ['111', '222'], 'Website': ['b.123solar.com', 'bd.123solar.com']}} </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload