0

So I need to make function that return reversed dictionary

this is what I tried:

def kaanna(sanakirja: dict):
    uusi = {}

    for avain, arvo in sanakirja.items():
        uusi[arvo] = avain

    print(uusi)

s = {1: 10, 2: 20, 3: 30, 4: 40}

kaanna(s)

print(s)

But this only return this same s dictionary and its should retunr it like this {10:1, 20:2, 30:3 etc.}

Barmar
  • 741,623
  • 53
  • 500
  • 612
F1ck
  • 1
  • 1

8 Answers8

1

You can reverse a KV pair in a dict with a dictionary comprehension.

s = {1: 10, 2: 20}
new_s = { v:k for k, v in s.items()}
print(new_s) # prints {10: 1, 20: 2}
1

There is no return from kaanna, nor a reassignment of s. This would work as you expect

def kaanna(sanakirja: dict):
    uusi = {}

    for avain, arvo in sanakirja.items():
        uusi[arvo] = avain

    return uusi

s = {1: 10, 2: 20, 3: 30, 4: 40}

s = kaanna(s)
print(s)

However, you can simplify this by using dictionary comprehension like so

s = {1: 10, 2: 20, 3: 30, 4: 40}
s_reversed = {v: k for k,v in s.items()}

s_reversed
{10: 1, 20: 2, 30: 3, 40: 4}
Joe Carboni
  • 421
  • 1
  • 6
0

When you print(s), you will see exactly the same dictionary as s was originally because none of your code modifies s. The print(uusi) shows the new dictionary you created. However, you say you want to return the dictionary, so you should change print(uusi) to return uusi. Then you can do something like print(kaanna(s)) to see the new dict.

I suggest you read more about return.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
0

You are not returning the output of that function, Try below code

def kaanna(sanakirja: dict):

    uusi = {}

    for avain, arvo in sanakirja.items():
        uusi[arvo] = avain

    print(uusi)
    return uusi

s = {1: 10, 2: 20, 3: 30, 4: 40}

s = kaanna(s)

print(s)
Manoj biroj
  • 288
  • 1
  • 6
0

You can do this nicely in a dict comprehension to avoid the need for a whole function (though other comments here are right, you forget to return the new dict at the end of your function)

rev = {val: key for key, val in s.items()}

if there are multiple val with the same value then the last one in the original dict will have priority

Richard Boyne
  • 327
  • 1
  • 7
0

You have to print the newly generated dictionary (the reversed one), not the original one:

def kaanna(sanakirja: dict):
    uusi = {}

    for avain, arvo in sanakirja.items():
        uusi[arvo] = avain

    return(uusi)

s = {1: 10, 2: 20, 3: 30, 4: 40}

new_s = kaanna(s)

print(new_s)
HuLu ViCa
  • 5,077
  • 10
  • 43
  • 93
0

Your code was close. "uusi" was only printing the reversed dictionary, not actually modifying the dictionary "s". You will then need to instead of just calling the function, set "s" equal to the function call.

def kaanna(sanakirja: dict):
uusi = {}

for avain, arvo in sanakirja.items():
    uusi[arvo] = avain

    return(uusi)

s = {1: 10, 2: 20, 3: 30, 4: 40}

s=kaanna(s)

print(s)

This worked for me.

zuro
  • 1
  • 1
0
s = {1: 10, 2: 20, 3: 30, 4: 40} 

def kaanna(sanakirja: dict): 
    return dict(reversed(list(sanakirja.items())))

kaanna(s)

{4: 40, 3: 30, 2: 20, 1: 10}

fanzhefu
  • 46
  • 2