Search code examples
pythondictionaryargskeyword-argument

Convert keys and values


I have a problem with the dictionary

I don't know how to convert keys to values

I tried to find way to solve this problem on the internet but I didn't understand

Expect: "I": 1 => 1:"I"

    convert = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000,}
    new_dict ={}
    for k,v in convert.items():
        new_dict.update({v,k})

Solution

  • args and kwargs are totally different things and related to something else not values and keys of dictionary..

    Now, for what you're asking, you can check this code to achieve what you were asking,

    Code Syntax

    convy = {value: key for key, value in convert.items()}
    

    Output

    {1: 'I', 5: 'V', 10: 'X', 50: 'L', 100: 'C', 500: 'D', 1000: 'M', 4: 'IV', 9: 'IX', 40: 'XL', 90: 'XC', 400: 'CD', 900: 'CM'}
    
    [Program finished]