Sorting Python list contating dicts
Question: I am looking for the fastest way to re-factor that following list which
contains dicts as items

    
    
    [{u'domain': u'1d663096.bestapp243.biz',
      u'flag_char_code': u'DR',
      u'flag_hex': u'8081',
      u'identifier': u'0000000002264A00',
      u'indicator': u'Snd',
      u'ip': u'172.30.133.105',
      u'proto': u'UDP',
      u'r_q': u'R Q',
      u'raw': u'10/12/2014 11:20:27 AM 1114 PACKET  0000000002264A00 UDP Snd 172.30.133.105  aba2 R Q [8081   DR  NOERROR] A      .1d663096.bestapp243.biz.',
      u'record': u'A',
      u'status': u'NOERROR',
      u'thread_id': u'1114',
      u'timestamp': u'2014-10-12T11:20:27',
      u'xid': u'aba2'},
     {u'domain': u'1d663096.bestapp243.biz',
      u'flag_char_code': u'DR',
      u'flag_hex': u'8081',
      u'identifier': u'0000000002264A00',
      u'indicator': u'Snd',
      u'ip': u'172.30.133.105',
      u'proto': u'UDP',
      u'r_q': u'R Q',
      u'raw': u'10/12/2014 11:20:27 AM 1114 PACKET  0000000002264A00 UDP Snd 172.30.133.105  aba2 R Q [8081   DR  NOERROR] A      .1d663096.bestapp243.biz.',
      u'record': u'A',
      u'status': u'NOERROR',
      u'thread_id': u'1114',
      u'timestamp': u'2014-10-12T11:20:27',
      u'xid': u'aba2'},
     {u'domain': u'mgames.cf',
      u'flag_char_code': u'DR',
      u'flag_hex': u'8081',
      u'identifier': u'000000000220ED40',
      u'indicator': u'Snd',
      u'ip': u'172.30.138.116',
      u'proto': u'UDP',
      u'r_q': u'R Q',
      u'raw': u'10/13/2014 2:31:46 PM 110C PACKET  000000000220ED40 UDP Snd 172.30.138.116  f957 R Q [8081   DR  NOERROR] A      .mgames.cf.',
      u'record': u'A',
      u'status': u'NOERROR',
      u'thread_id': u'110C',
      u'timestamp': u'2014-10-13T14:31:46',
      u'xid': u'f957'},
      {u'domain': u'google.com',
      u'flag_char_code': u'DR',
      u'flag_hex': u'8081',
      u'identifier': u'0000000002264A00',
      u'indicator': u'Snd',
      u'ip': u'172.30.133.105',
      u'proto': u'UDP',
      u'r_q': u'R Q',
      u'raw': u'10/12/2014 11:20:27 AM 1114 PACKET  0000000002264A00 UDP Snd 172.30.133.105  aba2 R Q [8081   DR  NOERROR] A      .google.com.',
      u'record': u'A',
      u'status': u'NOERROR',
      u'thread_id': u'1114',
      u'timestamp': u'2014-10-12T11:20:27',
      u'xid': u'aba2'},
     {u'domain': u'qwe.domainsworkingsdromms.com',
      u'flag_char_code': u'DR',
      u'flag_hex': u'8381',
      u'identifier': u'030E8D88',
      u'indicator': u'Snd',
      u'ip': u'172.27.29.77',
      u'proto': u'UDP',
      u'r_q': u'R Q',
      u'raw': u'10/14/2014 10:37:13 AM 17E0 PACKET  030E8D88 UDP Snd 172.27.29.77    80eb R Q [8381   DR NXDOMAIN] A     .qwe.domainsworkingsdromms.com.',
      u'record': u'A',
      u'status': u'NXDOMAIN',
      u'thread_id': u'17E0',
      u'timestamp': u'2014-10-14T10:37:13',
      u'xid': u'80eb'}]
    

to output something like:

    
    
    {
      '172.30.133.105': {
        '1d663096.bestapp243.biz':
          [
            {u'domain': u'1d663096.bestapp243.biz',
              u'flag_char_code': u'DR',
              u'flag_hex': u'8081',
              u'identifier': u'0000000002264A00',
              u'indicator': u'Snd',
              u'ip': u'172.30.133.105',
              u'proto': u'UDP',
              u'r_q': u'R Q',
              u'raw': u'10/12/2014 11:20:27 AM 1114 PACKET  0000000002264A00 UDP Snd 172.30.133.105  aba2 R Q [8081   DR  NOERROR] A      .1d663096.bestapp243.biz.',
              u'record': u'A',
              u'status': u'NOERROR',
              u'thread_id': u'1114',
              u'timestamp': u'2014-10-12T11:20:27',
              u'xid': u'aba2'},
            {u'domain': u'1d663096.bestapp243.biz',
              u'flag_char_code': u'DR',
              u'flag_hex': u'8081',
              u'identifier': u'0000000002264A00',
              u'indicator': u'Snd',
              u'ip': u'172.30.133.105',
              u'proto': u'UDP',
              u'r_q': u'R Q',
              u'raw': u'10/12/2014 11:20:27 AM 1114 PACKET  0000000002264A00 UDP Snd 172.30.133.105  aba2 R Q [8081   DR  NOERROR] A      .1d663096.bestapp243.biz.',
              u'record': u'A',
              u'status': u'NOERROR',
              u'thread_id': u'1114',
              u'timestamp': u'2014-10-12T11:20:27',
              u'xid': u'aba2'},
          ],
        'google.com':
          [
            {u'domain': u'google.com',
            u'flag_char_code': u'DR',
            u'flag_hex': u'8081',
            u'identifier': u'0000000002264A00',
            u'indicator': u'Snd',
            u'ip': u'172.30.133.105',
            u'proto': u'UDP',
            u'r_q': u'R Q',
            u'raw': u'10/12/2014 11:20:27 AM 1114 PACKET  0000000002264A00 UDP Snd 172.30.133.105  aba2 R Q [8081   DR  NOERROR] A      .google.com.',
            u'record': u'A',
            u'status': u'NOERROR',
            u'thread_id': u'1114',
            u'timestamp': u'2014-10-12T11:20:27',
            u'xid': u'aba2'}
          ]
      },
      '172.30.138.116': {
        'mgames.cf': 
          [
           {u'domain': u'mgames.cf',
            u'flag_char_code': u'DR',
            u'flag_hex': u'8081',
            u'identifier': u'000000000220ED40',
            u'indicator': u'Snd',
            u'ip': u'172.30.138.116',
            u'proto': u'UDP',
            u'r_q': u'R Q',
            u'raw': u'10/13/2014 2:31:46 PM 110C PACKET  000000000220ED40 UDP Snd 172.30.138.116  f957 R Q [8081   DR  NOERROR] A      .mgames.cf.',
            u'record': u'A',
            u'status': u'NOERROR',
            u'thread_id': u'110C',
            u'timestamp': u'2014-10-13T14:31:46',
            u'xid': u'f957'}        
          ]
      }
    }
    

Should I divide them into two lists, the 1st one will have ips as keys then
iterate over them to fix the domains as keys for the sub dicts ?

Any ideas would be appreciated. Thanks


Answer: If you need to re-factor, know the re-factored structure and do that
explicitly

    
    
    foo = {}
    for rec in data:
        foo[rec['ip']] = {}
        foo[rec['ip']][rec['domain']] = rec 
    

**Note** , if you are not to orthodox in using `dict` and rather open for
`defaultdict`, `autovivication` can work wonder here

    
    
    >>> from collections import defaultdict
    >>> def tree(): return defaultdict(tree) 
    >>> foo = tree()
    >>> for rec in data:
        foo[rec['ip']][rec['domain']] = rec
    

**Example**

    
    
    >>> import pprint
    >>> pprint.pprint(foo)
    {u'172.27.29.77': {u'qwe.domainsworkingsdromms.com': {u'domain': u'qwe.domainsworkingsdromms.com',
                                                          u'flag_char_code': u'DR',
                                                          u'flag_hex': u'8381',
                                                          u'identifier': u'030E8D88',
                                                          u'indicator': u'Snd',
                                                          u'ip': u'172.27.29.77',
                                                          u'proto': u'UDP',
                                                          u'r_q': u'R Q',
                                                          u'raw': u'10/14/2014 10:37:13 AM 17E0 PACKET  030E8D88 UDP Snd 172.27.29.77    80eb R Q [8381   DR NXDOMAIN] A     .qwe.domainsworkingsdromms.com.',
                                                          u'record': u'A',
                                                          u'status': u'NXDOMAIN',
                                                          u'thread_id': u'17E0',
                                                          u'timestamp': u'2014-10-14T10:37:13',
                                                          u'xid': u'80eb'}},
     u'172.30.133.105': {u'google.com': {u'domain': u'google.com',
                                         u'flag_char_code': u'DR',
                                         u'flag_hex': u'8081',
                                         u'identifier': u'0000000002264A00',
                                         u'indicator': u'Snd',
                                         u'ip': u'172.30.133.105',
                                         u'proto': u'UDP',
                                         u'r_q': u'R Q',
                                         u'raw': u'10/12/2014 11:20:27 AM 1114 PACKET  0000000002264A00 UDP Snd 172.30.133.105  aba2 R Q [8081   DR  NOERROR] A      .google.com.',
                                         u'record': u'A',
                                         u'status': u'NOERROR',
                                         u'thread_id': u'1114',
                                         u'timestamp': u'2014-10-12T11:20:27',
                                         u'xid': u'aba2'}},
     u'172.30.138.116': {u'mgames.cf': {u'domain': u'mgames.cf',
                                        u'flag_char_code': u'DR',
                                        u'flag_hex': u'8081',
                                        u'identifier': u'000000000220ED40',
                                        u'indicator': u'Snd',
                                        u'ip': u'172.30.138.116',
                                        u'proto': u'UDP',
                                        u'r_q': u'R Q',
                                        u'raw': u'10/13/2014 2:31:46 PM 110C PACKET  000000000220ED40 UDP Snd 172.30.138.116  f957 R Q [8081   DR  NOERROR] A      .mgames.cf.',
                                        u'record': u'A',
                                        u'status': u'NOERROR',
                                        u'thread_id': u'110C',
                                        u'timestamp': u'2014-10-13T14:31:46',
                                        u'xid': u'f957'}}}
    

