I have 2 nested dictionaries in Python that have this format:
1166869: {'probL2': '0.000', 'probL1': '0.000', 'pronNDiff_site': '1.000', 'StateBin': '0', 'chr': 'chrX', 'rangehist': '59254000-59255000', 'start_bin': '59254000', 'countL2': '4', 'countL1': '0'}
1166870: {'probL2': '0.148', 'probL1': '0.000', 'pronNDiff_site': '0.851', 'StateBin': '0', 'chr': 'chr2', 'rangehist': '59254000-59255000', 'start_bin': '59255000', 'countL2': '5', 'countL1': '15'}
1166871: {'probL2': '0.000', 'probL1': '0.000', 'pronNDiff_site': '1.000', 'StateBin': '0', 'chr': 'chrY', 'rangehist': '59290000-59291000', 'start_bin': '59290000', 'countL2': '1', 'countL1': '2'}
where 1166869, 1166870 and 1166871 represent a line in a file from where I read the data, and the rest of the keys are the data itself.
Now I want to make a list where I store all the different values in the key “chr” because there are some repeated ones.
How can I go through the dictionary and make the comparison between the 2 values? This code is not working:
for k in range(len(file_dict)):
for j in range(len(file_dict)-1):
if (file_dict[j]["chr"] != file_dict[k]["chr"]):
list_chr.append(file_dict[j]["chr"])
Use a set, and just all the items in one go:
This uses a set comprehension to generate your set in one line of code.
Set comprehensions were introduced in Python 2.7; in earlier versions use:
In Python 3, you’d need to replace
.itervalues()by.values().Your own code doesn’t work because python dictionaries are not lists; you don’t retrieve values by index, but by key. You’d have to change it to:
but that is really inefficient, not to mention incorrect.