A neat trick for switching keys and values in a python dictionary. You'll love it!




[ad_1]

This is a nice little way of using a dictionary comprehension to switch dictionary keys and values in python.

WANT TO LEARN PYTHON?
You can sign up for my course at http://www.datainsight.news
Use discount code B98612B8D9 to get a year’s membership for just $5

How to Learn Data Science for Free. My YouTube video:
https://youtu.be/eTxyviU0Ddo

My favourite books (affiliate link) https://www.amazon.co.uk/shop/pythonprogrammer

https://www.instagram.com/gilesmcmullen/
https://twitter.com/GilesMcMullen

👌 SUBSCRIBE to ME!👌
https://www.youtube.com/channel/UC68KSmHePPePCjW4v57VPQg?sub_confirmation=1

Source


[ad_2]

Comment List

  • Python Programmer
    December 6, 2020

    Did it and just felt a little warming pleasure :). Didn't know about dictionary comprehensions, just tried and it worked. Thanks Giles!

  • Python Programmer
    December 6, 2020

    It's not a perfect solution. Keys have to be unique.

  • Python Programmer
    December 6, 2020

    new_d = {d[i]:i for i in d.keys()}. EDIT just saw the end where you have your version. Can’t see a practical purpose for it though. All it takes is one duplicate value to give an undesirable result. Defeats the purpose of a dictionary .

  • Python Programmer
    December 6, 2020

    My answer is a little briefer… and clearer, I think
    my_dictionary = {mydictionary[key]:key for key in my_dictionary}

  • Python Programmer
    December 6, 2020

    I tried it when you told us to pause and I used exactly the same approach 🙂

  • Python Programmer
    December 6, 2020

    won't work if value is not immutable i.e list, set, etc

  • Python Programmer
    December 6, 2020

    Hey, Quick question. I’m interested in buying the new MacBook with M1 chip, What would you suggest me to go for? The MacBook Air (8Gb ram 256ssd) or MacBook Pro (8Gb ram 256ssd)?

  • Python Programmer
    December 6, 2020

    this guy is great. he does more video editing than actual python lol. amazing view.

  • Python Programmer
    December 6, 2020

    rev = {j, i for i,j in zip(dic.keys(), dic.values())}

    Edit: forgot about items. It's much more elegant

  • Python Programmer
    December 6, 2020

    Please post more content more often

  • Python Programmer
    December 6, 2020

    Assuming the dictionary has unique values …

  • Python Programmer
    December 6, 2020

    Neat

  • Python Programmer
    December 6, 2020

    More of these kind of puzzles please.

  • Python Programmer
    December 6, 2020

    Does this run through the dicitionary No keys ^2 times? ie is there a big performance cost?

  • Python Programmer
    December 6, 2020

    I did love it! read my mind

  • Python Programmer
    December 6, 2020

    Hi everyone!. Some could tell me, an specific application of this idea?

  • Python Programmer
    December 6, 2020

    I've heard of list comprehension, didn't know you could do it with dictionaries… dict comprehension? haha

  • Python Programmer
    December 6, 2020

    How many programing languages do you know

  • Python Programmer
    December 6, 2020

    Did I see a change in the thumbnail🤔, I thought I saw,"can you display your python version?"🧐

Write a comment