A
A
AkaMaloy2021-08-02 17:04:33
Python
AkaMaloy, 2021-08-02 17:04:33

I need to make the key value a dictionary, how to do it?

I have a dictionary
a = {'Spartacus': 2, 'Zenith': 2, 'Locomotive': 2} And I need to make each value a dictionary.
That is, it should turn out:
a = {'Spartacus': [2], ' Zenit': [2], 'Locomotive': [2]}

But new keys and values ​​can be added to the dictionary
For example:
{'Spartak': 2, 'Zenith': 2, 'Locomotive': 2, 'CSKA': 2}

And I won't know for sure what these values ​​and keys will be

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Michael, 2021-08-02
@AkaMaloy

Based on the above example:

a = {'Спартак': 2, 'Зенит': 2, 'Локомотив': 2}

for key in a:
    a[key] = [a[key]]

print(a) # {'Спартак': [2], 'Зенит': [2], 'Локомотив': [2]}

There is a typo in the author's TOR: the example describes conversion to a list, not a dictionary.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question