N
N
nano_e_t_42015-11-23 08:15:00
Python
nano_e_t_4, 2015-11-23 08:15:00

How to equate key and value?

Hello everyone
Tell me, who knows (may have come across) - how to equate a key to a value in a dictionary?
That is, for example, there is a dictionary a = {'one' : 'uno', 'two' : 'duo'}, you need to do one = uno, two = duo

Answer the question

In order to leave comments, you need to log in

3 answer(s)
Z
zed, 2015-11-23
@nano_e_t_4

Elementary same:

for key in a:
    print '%s = %s' % (key, a[key])

T
tsarevfs, 2015-11-23
@tsarevfs

Yes, just create a new dictionary with the desired elements. Iterate over the values ​​in the original and add them to the new one as keys and values.
b = {v : v for v in a.values()}

D
Dmitry Demidov, 2015-11-23
@ptitca_zu

tsarevfs wrote you almost correctly (except that you need to go by keys, not by values)
But why you put 'c' as a key in your code is a mystery.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question