With the below data which convert list to dict:
store=[['hostname ', ' ABCD'], ['fsystem ', ' /dev/sdb'], ['actual_size ', ' 2.5T'], ['hostname ', ' XYZ'], ['fsystem ', ' /dev/sdb'], ['actual_size ', ' 2.5T']]
Please help/advise me as how I can convert the above List to Dict?
I tried with below code but it is not giving me expected output
for i in store:
print(dict(zip(i,i)))
2 Answers 2
import json
b={'description': [['hostname ', ' ABC'], ['fsystem ', ' /dev/sdb'], ['actual_size ', ' 2.5T'], ['used_disk ', ' 486G'], ['avail_disk ', ' 1.9T'], ['percentage ', ' 21'], ['mount_disk ', ' /data'], ['hostname ', ' XYZ'], ['fsystem ', ' /dev/sdb'], ['actual_size ', ' 2.5T'], ['used_disk ', ' 533G'], ['avail_disk ', ' 1.8T'], ['percentage ', ' 23%'], ['mount_disk ', ' /data'], ['hostname ', ' MNOP'], ['fsystem ', ' /dev/sdb'], ['actual_size ', ' 2.5T'], ['used_disk ', ' 664G'], ['avail_disk ', ' 1.7T'], ['percentage ', ' 29%'], ['mount_disk ', ' /data']]}
b=(b['description'])
size = len(b)
idx_list = [idx + 1 for idx, val in enumerate(b) if 'mount_disk ' in val] #note the blank space!!!!
res = [dict(b[i: j]) for i, j in
zip([0] + idx_list, idx_list +
([size] if idx_list[-1] != size else []))]
JSONarray=json.dumps(res) # or json.dump(...) to write the output in a new file
to check your new array of JSON objects:
print("now you have this list of JSON objects : " + str(res))
I would like to note that any feedback would be really appreciated, even just to clearify if you already have already solved your issue.
the split in multiple lists is adapted from: https://www.geeksforgeeks.org/python-split-list-into-lists-by-particular-value/
Edit:
I've now looked at your other questions in stackoverflow which make me think you weren't asking for fixing your inner lists to dicts but just a plain dict to json conversion. I can't understand how a json in that format may help you, but if that's the case you should specify it in your question. Looking at that dict format is very misleading.
Edit2
just:
store=[['hostname ', ' ABCD'], ['fsystem ', ' /dev/sdb'], ['actual_size ', ' 2.5T'], ['hostname ', ' XYZ'], ['fsystem ', ' /dev/sdb'], ['actual_size ', ' 2.5T']]
size = len(store)
idx_list = [idx + 1 for idx, val in enumerate(store) if 'actual_size ' in val] #note the blank space!!!!
storeNew = [dict(store[i: j]) for i, j in zip([0] + idx_list, idx_list + ([size] if idx_list[-1] != size else []))]
storeNew #[{'actual_size ': ' 2.5T', 'fsystem ': ' /dev/sdb', 'hostname ': ' ABCD'},{'actual_size ': ' 2.5T', 'fsystem ': ' /dev/sdb', 'hostname ': ' XYZ'}]
6 Comments
storeNew is a list which contains 2 dictionaries.I have noticed that you have duplicate keys but you cannot add duplicate keys to dictionary, so you have to iterate the 2 sub lists and create 2 dictionaries.Then i have appended the 2 dictionaries in a list of dictionaries.I hope i understand the question.Check my answer.
store=[ ['hostname ','ABCD','fsystem ','/dev/sdb','actual_size ','2.5T'],
['hostname ','XYZ','fsystem ','/dev/sdb','actual_size ','2.5T']
]
def convert_to_dictionary(a):
it = iter(a)
res_dct = dict(zip(it, it))
return res_dct
dictionaries = []
#iterate each sublist in store list
for lst in store:
dictionary = convert_to_dictionary(lst)
dictionaries.append(dictionary);
for di in dictionaries:
print(di)
dict{<key>:<value>}? For example, you want this?{'hostname': 'ABCD'}?print({i[0]: i[1] for i in store})