This is a test version of Biostars. For the public version, visit https://www.biostars.org.
How to assign value to element in nested dictionary in Python

Hi all!

I have a question. I have the following nested dictionary:

{'Cluster 0': {'samples': {'SOL 1_3'}}}

And I would like to modify the dictionary to include the abundance per sample:

{'Cluster 0': {'samples': {'SOL 1_3':7}}}

I tried this command:

dct_cluster_sol[current_cluster]['samples']['SOL_1_3'] = 7

And it gives this error:

 dct_cluster_sol[current_cluster]['samples']['SOL_1_3'] = 7
 TypeError: 'set' object does not support item assignment

Do you know how to solve it?

Thank you!

dictionary python

{'Cluster 0': {'samples': {'SOL 1_3'}}} has the mapping {str -> { str -> set }}. You probably only need {'Cluster 0': {'samples': dict()}}

Thanks! It worked!

0 answers

No answers yet.

Log in to answer this question.