1

Hi guys just wondering if it is possible to convert the list below to a dict, i know it is originally a dict but it is showing up now as a list, if it can be converted that would be the easiest method

i have tried d = dict(zip(*[iter(list[0])]*2)) but this does not work

this is the list :

[[{'identityb32': 'qa46imyg4njipjsgpudn7vnn2zub2tec', 'pubdate': '2014-07-07', 'dirport': '80', 'ip': '18.238.2.85', 'orport': '443', 'identityhash': '8039e43306e35287a6467d06dfd5add6681d4c82', 'nick': 'Onions', 'version': 'Tor 0.2.3.25', 'flags': ['Exit', 'Fast', 'HSDir', 'Running', 'V2Dir', 'Valid'], 'identity': '\x809\xe43\x06\xe3R\x87\xa6F}\x06\xdf\xd5\xad\xd6h\x1dL\x82', 'digest': 'lh2LprFLd0bdvtIfPkz58//mgZk', 'pubtime': '09:37:21'}, {'identityb32': 'qbwfsrn3y5mmcnshotg77clvxjufngso', 'pubdate': '2014-07-07', 'dirport': '80', 'ip': '109.120.148.60', 'orport': '443', 'identityhash': '806c5945bbc758c1364774cdff8975ba68569a4e', 'nick': 'Reactor', 'version': 'Tor 0.2.4.22', 'flags': ['Exit', 'Fast', 'HSDir', 'Running', 'V2Dir', 'Valid'], 'identity': '\x80lYE\xbb\xc7X\xc16Gt\xcd\xff\x89u\xbahV\x9aN', 'digest': 'bDAS/RN7q4injGzkIXobcsSwS6c', 'pubtime': '08:55:57'}, {'identityb32': 'qcrds2ps562vf3ctnuoob4lgtmvt4zau', 'pubdate': '2014-07-07', 'dirport': '9030', 'ip': '195.154.12.66', 'orport': '9001', 'identityhash': '80a23969f2efb552ec536d1ce0f1669b2b3e6414', 'nick': 'han1', 'version': 'Tor 0.2.4.22', 'flags': ['Fast', 'HSDir', 'Named', 'Running', 'Stable', 'V2Dir', 'Valid'], 'identity': '\x80\xa29i\xf2\xef\xb5R\xecSm\x1c\xe0\xf1f\x9b+>d\x14', 'digest': 'FoPXNW58WS4eoCWB/eIK7j5yS4E', 'pubtime': '01:09:22'}], [{'identityb32': 'rzjbswyebloufopae2j46a6272wrwlae', 'pubdate': '2014-07-07', 'dirport': '80', 'ip': '77.247.181.164', 'orport': '443', 'identityhash': '8e52195b040add42b9e02693cf03dafead1b2c04', 'nick': 'rainbowwarrior', 'version': 'Tor 0.2.5.3-alpha', 'flags': ['Exit', 'Fast', 'Guard', 'HSDir', 'Running', 'Stable', 'Unnamed', 'V2Dir', 'Valid'], 'identity': '\x8eR\x19[\x04\n\xddB\xb9\xe0&\x93\xcf\x03\xda\xfe\xad\x1b,\x04', 'digest': '67A4paMg3mQUqHei8jNSWR2QWsg', 'pubtime': '08:29:31'}, {'identityb32': 'rzniakcq6yj3xdbk766e4yuspkhm2bq2', 'pubdate': '2014-07-07', 'dirport': '9030', 'ip': '212.116.188.158', 'orport': '9001', 'identityhash': '8e5a802850f613bb8c2affbc4e62927a8ecd061a', 'nick': '7sYiz', 'version': 'Tor 0.2.3.25', 'flags': ['Exit', 'Fast', 'Guard', 'HSDir', 'Named', 'Running', 'Stable', 'V2Dir', 'Valid'], 'identity': '\x8eZ\x80(P\xf6\x13\xbb\x8c*\xff\xbcNb\x92z\x8e\xcd\x06\x1a', 'digest': '5+O3G9AMJujmUVxO3Fhz7MSPpHI', 'pubtime': '12:34:28'}, {'identityb32': 'rzs3murcnwhdeyj6blsfj7ipltbpmn44', 'pubdate': '2014-07-07', 'dirport': '9030', 'ip': '176.31.122.216', 'orport': '9001', 'identityhash': '8e65b652226d8e32613e0ae454fd0f5cc2f6379c', 'nick': 'somewhere', 'version': 'Tor 0.2.4.22', 'flags': ['Fast', 'HSDir', 'Running', 'Stable', 'Unnamed', 'V2Dir', 'Valid'], 'identity': '\x8ee\xb6R"m\x8e2a>\n\xe4T\xfd\x0f\\\xc2\xf67\x9c', 'digest': 'lEgmmiJcOvzsib9fZjgaxaSG6to', 'pubtime': '03:27:26'}]]

if not is there anyway to retrieve data from the list for example to retrieve all the data related to identityhash for example ?

ideally converting this back to a dict will be the best solution

Thanks

3
  • the inner list contains a dict Commented Jul 7, 2014 at 18:29
  • 1
    @user29644021, you have more than on sublist so the answer you accepted access just one and ignores all the other dicts in your list. Commented Jul 7, 2014 at 19:03
  • @user29644021 you are still only getting half the dicts Commented Jul 7, 2014 at 19:18

2 Answers 2

3
list=[[{'identityb32': 'qa46imyg4njipjsgpudn7vnn2zub2tec', 'pubdate': '2014-07-07', 'dirport': '80', 'ip': '18.238.2.85', 'orport': '443', 'identityhash': '8039e43306e35287a6467d06dfd5add6681d4c82', 'nick': 'Onions', 'version': 'Tor 0.2.3.25', 'flags': ['Exit', 'Fast', 'HSDir', 'Running', 'V2Dir', 'Valid'], 'identity': '\x809\xe43\x06\xe3R\x87\xa6F}\x06\xdf\xd5\xad\xd6h\x1dL\x82', 'digest': 'lh2LprFLd0bdvtIfPkz58//mgZk', 'pubtime': '09:37:21'}, {'identityb32': 'qbwfsrn3y5mmcnshotg77clvxjufngso', 'pubdate': '2014-07-07', 'dirport': '80', 'ip': '109.120.148.60', 'orport': '443', 'identityhash': '806c5945bbc758c1364774cdff8975ba68569a4e', 'nick': 'Reactor', 'version': 'Tor 0.2.4.22', 'flags': ['Exit', 'Fast', 'HSDir', 'Running', 'V2Dir', 'Valid'], 'identity': '\x80lYE\xbb\xc7X\xc16Gt\xcd\xff\x89u\xbahV\x9aN', 'digest': 'bDAS/RN7q4injGzkIXobcsSwS6c', 'pubtime': '08:55:57'}, {'identityb32': 'qcrds2ps562vf3ctnuoob4lgtmvt4zau', 'pubdate': '2014-07-07', 'dirport': '9030', 'ip': '195.154.12.66', 'orport': '9001', 'identityhash': '80a23969f2efb552ec536d1ce0f1669b2b3e6414', 'nick': 'han1', 'version': 'Tor 0.2.4.22', 'flags': ['Fast', 'HSDir', 'Named', 'Running', 'Stable', 'V2Dir', 'Valid'], 'identity': '\x80\xa29i\xf2\xef\xb5R\xecSm\x1c\xe0\xf1f\x9b+>d\x14', 'digest': 'FoPXNW58WS4eoCWB/eIK7j5yS4E', 'pubtime': '01:09:22'}], [{'identityb32': 'rzjbswyebloufopae2j46a6272wrwlae', 'pubdate': '2014-07-07', 'dirport': '80', 'ip': '77.247.181.164', 'orport': '443', 'identityhash': '8e52195b040add42b9e02693cf03dafead1b2c04', 'nick': 'rainbowwarrior', 'version': 'Tor 0.2.5.3-alpha', 'flags': ['Exit', 'Fast', 'Guard', 'HSDir', 'Running', 'Stable', 'Unnamed', 'V2Dir', 'Valid'], 'identity': '\x8eR\x19[\x04\n\xddB\xb9\xe0&\x93\xcf\x03\xda\xfe\xad\x1b,\x04', 'digest': '67A4paMg3mQUqHei8jNSWR2QWsg', 'pubtime': '08:29:31'}, {'identityb32': 'rzniakcq6yj3xdbk766e4yuspkhm2bq2', 'pubdate': '2014-07-07', 'dirport': '9030', 'ip': '212.116.188.158', 'orport': '9001', 'identityhash': '8e5a802850f613bb8c2affbc4e62927a8ecd061a', 'nick': '7sYiz', 'version': 'Tor 0.2.3.25', 'flags': ['Exit', 'Fast', 'Guard', 'HSDir', 'Named', 'Running', 'Stable', 'V2Dir', 'Valid'], 'identity': '\x8eZ\x80(P\xf6\x13\xbb\x8c*\xff\xbcNb\x92z\x8e\xcd\x06\x1a', 'digest': '5+O3G9AMJujmUVxO3Fhz7MSPpHI', 'pubtime': '12:34:28'}, {'identityb32': 'rzs3murcnwhdeyj6blsfj7ipltbpmn44', 'pubdate': '2014-07-07', 'dirport': '9030', 'ip': '176.31.122.216', 'orport': '9001', 'identityhash': '8e65b652226d8e32613e0ae454fd0f5cc2f6379c', 'nick': 'somewhere', 'version': 'Tor 0.2.4.22', 'flags': ['Fast', 'HSDir', 'Running', 'Stable', 'Unnamed', 'V2Dir', 'Valid'], 'identity': '\x8ee\xb6R"m\x8e2a>\n\xe4T\xfd\x0f\\\xc2\xf67\x9c', 'digest': 'lEgmmiJcOvzsib9fZjgaxaSG6to', 'pubtime': '03:27:26'}]]
d = {}
for dic in l[0]:
    d.update(dic)
print d
{'identityb32': 'qcrds2ps562vf3ctnuoob4lgtmvt4zau', 'pubdate': '2014-07-07', 'ip': '195.154.12.66', 'orport': '9001', 'digest': 'FoPXNW58WS4eoCWB/eIK7j5yS4E', 'dirport': '9030', 'identityhash': '80a23969f2efb552ec536d1ce0f1669b2b3e6414', 'nick': 'han1', 'version': 'Tor 0.2.4.22', 'flags': ['Fast', 'HSDir', 'Named', 'Running', 'Stable', 'V2Dir', 'Valid'], 'identity': '\x80\xa29i\xf2\xef\xb5R\xecSm\x1c\xe0\xf1f\x9b+>d\x14', 'pubtime': '01:09:22'}

You have more than one sublist in your list so you may need to zip and update, but with you having dicts using the same keys so you will be losing certain values when you update to a new dict.

If you want to flatten all the dicts into a single list you and create multiple dicts to keep all the values or get whatever keys values you need in a list comp:

flattened= ([x for y in  l for x in y])

This will get all the hashes from all dicts:

print [x.get('identityhash') for x in flattened]

['8039e43306e35287a6467d06dfd5add6681d4c82', '806c5945bbc758c1364774cdff8975ba68569a4e', '80a23969f2efb552ec536d1ce0f1669b2b3e6414', '8e52195b040add42b9e02693cf03dafead1b2c04', '8e5a802850f613bb8c2affbc4e62927a8ecd061a', '8e65b652226d8e32613e0ae454fd0f5cc2f6379c']
Sign up to request clarification or add additional context in comments.

3 Comments

This is needlessly complicated.
@NateMara, there are more elements in the list, not just one dict, you are just accessing one.
AttributeError: 'str' object has no attribute 'get'
1

You will lose data if you combine the dicts, as the inner list contains multiple dicts with the same keys. If you just want the identity hashes in a list, you can use a list comprehension:

>>> data = [[{'identityb32': 'qa46imyg4njipjsgpudn7vnn2zub2tec', 'pubdate': '2014-07-07', 'dirport': '80', 'ip': '18.238.2.85', 'orport': '443', 'identityhash': '8039e43306e35287a6467d06dfd5add6681d4c82', 'nick': 'Onions', 'version': 'Tor 0.2.3.25', 'flags': ['Exit', 'Fast', 'HSDir', 'Running', 'V2Dir', 'Valid'], 'identity': '\x809\xe43\x06\xe3R\x87\xa6F}\x06\xdf\xd5\xad\xd6h\x1dL\x82', 'digest': 'lh2LprFLd0bdvtIfPkz58//mgZk', 'pubtime': '09:37:21'}, {'identityb32': 'qbwfsrn3y5mmcnshotg77clvxjufngso', 'pubdate': '2014-07-07', 'dirport': '80', 'ip': '109.120.148.60', 'orport': '443', 'identityhash': '806c5945bbc758c1364774cdff8975ba68569a4e', 'nick': 'Reactor', 'version': 'Tor 0.2.4.22', 'flags': ['Exit', 'Fast', 'HSDir', 'Running', 'V2Dir', 'Valid'], 'identity': '\x80lYE\xbb\xc7X\xc16Gt\xcd\xff\x89u\xbahV\x9aN', 'digest': 'bDAS/RN7q4injGzkIXobcsSwS6c', 'pubtime': '08:55:57'}, {'identityb32': 'qcrds2ps562vf3ctnuoob4lgtmvt4zau', 'pubdate': '2014-07-07', 'dirport': '9030', 'ip': '195.154.12.66', 'orport': '9001', 'identityhash': '80a23969f2efb552ec536d1ce0f1669b2b3e6414', 'nick': 'han1', 'version': 'Tor 0.2.4.22', 'flags': ['Fast', 'HSDir', 'Named', 'Running', 'Stable', 'V2Dir', 'Valid'], 'identity': '\x80\xa29i\xf2\xef\xb5R\xecSm\x1c\xe0\xf1f\x9b+>d\x14', 'digest': 'FoPXNW58WS4eoCWB/eIK7j5yS4E', 'pubtime': '01:09:22'}], [{'identityb32': 'rzjbswyebloufopae2j46a6272wrwlae', 'pubdate': '2014-07-07', 'dirport': '80', 'ip': '77.247.181.164', 'orport': '443', 'identityhash': '8e52195b040add42b9e02693cf03dafead1b2c04', 'nick': 'rainbowwarrior', 'version': 'Tor 0.2.5.3-alpha', 'flags': ['Exit', 'Fast', 'Guard', 'HSDir', 'Running', 'Stable', 'Unnamed', 'V2Dir', 'Valid'], 'identity': '\x8eR\x19[\x04\n\xddB\xb9\xe0&\x93\xcf\x03\xda\xfe\xad\x1b,\x04', 'digest': '67A4paMg3mQUqHei8jNSWR2QWsg', 'pubtime': '08:29:31'}, {'identityb32': 'rzniakcq6yj3xdbk766e4yuspkhm2bq2', 'pubdate': '2014-07-07', 'dirport': '9030', 'ip': '212.116.188.158', 'orport': '9001', 'identityhash': '8e5a802850f613bb8c2affbc4e62927a8ecd061a', 'nick': '7sYiz', 'version': 'Tor 0.2.3.25', 'flags': ['Exit', 'Fast', 'Guard', 'HSDir', 'Named', 'Running', 'Stable', 'V2Dir', 'Valid'], 'identity': '\x8eZ\x80(P\xf6\x13\xbb\x8c*\xff\xbcNb\x92z\x8e\xcd\x06\x1a', 'digest': '5+O3G9AMJujmUVxO3Fhz7MSPpHI', 'pubtime': '12:34:28'}, {'identityb32': 'rzs3murcnwhdeyj6blsfj7ipltbpmn44', 'pubdate': '2014-07-07', 'dirport': '9030', 'ip': '176.31.122.216', 'orport': '9001', 'identityhash': '8e65b652226d8e32613e0ae454fd0f5cc2f6379c', 'nick': 'somewhere', 'version': 'Tor 0.2.4.22', 'flags': ['Fast', 'HSDir', 'Running', 'Stable', 'Unnamed', 'V2Dir', 'Valid'], 'identity': '\x8ee\xb6R"m\x8e2a>\n\xe4T\xfd\x0f\\\xc2\xf67\x9c', 'digest': 'lEgmmiJcOvzsib9fZjgaxaSG6to', 'pubtime': '03:27:26'}]]
>>> identityhash = [i.get('identityhash') for j in data for i in j]
>>> print(identityhash)
['8039e43306e35287a6467d06dfd5add6681d4c82','806c5945bbc758c1364774cdff8975ba68569a4e','80a269f2efb552ec536d1ce0f1669b2b3e6414','8e52195b040add42b9e02693cf03dafead1b2c04','8e5a802850f6bb8c2affbc4e62927a8ecd061a','8e65b652226d8e32613e0ae454fd0f5cc2f6379c']

7 Comments

there are more that data[0][0] dicts in the list so you do need a loop.
@PadraicCunningham I see that now.
still not accessing all dicts
Should now be accessing all dicts
now you have flattened the list as I already had in my answer ;)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.