0

Given dictionary is d1={"a":1,"b":2,"c":3,"d":4,"e":4} convert the same to {1:"a",2:"b",3:"c",4:["d","e"]}

I have tried and I just got reverse string using key value pair

dictionary={"a":1,"b":2,"c":2}
r_dictionary={value:key for key, value in dictionary.items()
martineau
  • 119,623
  • 25
  • 170
  • 301
Ashwini
  • 35
  • 3
  • @MartijnPieters Well – the answers in that dupe are still not what OP wants; apparently non-duplicates _shouldn't_ be lists. – AKX May 06 '22 at 14:45
  • 2
    @AKX: tough, that's not a good idea anyway. Better to stick with consistent value types (everything a list or nothing a list, don't mix and match). – Martijn Pieters May 06 '22 at 14:46
  • Yep, I'm perfectly aware it's not a good idea. Ah well, not going to reopen this anymore – I guess OP can add `{key: value[0] if len(value) == 1 else value for key, value in d.items()}` to those answers by themselves. – AKX May 06 '22 at 14:47
  • 1
    I also added [Reverse / invert a dictionary mapping](https://stackoverflow.com/q/483666), which has a few more answers on the subject (next to the straight-up-lets-asume-values-are-unique inversions). – Martijn Pieters May 06 '22 at 14:49
  • 1
    @AKX your solution is wrong you are checking the length of an integer with `len(value) == 1`. It will crash. And in fact, you should not use a short hand because you need to at least use 2 `if statements`. And pls open the question again as no question is answered what if there are duplicate values (there is 1 but it creates every value as a list). – Harshit Singh May 06 '22 at 15:23
  • @HarshitSingh: You are mistaken about no answers to a duplicate question handling duplicate dictionary values. For example *both* of the answers to [this one](https://stackoverflow.com/questions/44851342/reverse-a-dictionary-to-key-list-of-values) do. So I agree there's no reason to reopen this question. – martineau May 06 '22 at 15:30
  • @HarshitSingh my addition applies to the duplicates linked. – AKX May 06 '22 at 15:33
  • @martineau I understand what you are saying but clearly, he also wants a dictionary with no duplicate keys, and also you cannot even create one. See what he wants as output `{1:"a",2:"b",3:"c",4:["d", "e"]}` but every duplicate thing solution is giving him `{1:["a"],2:["b"],3:["c"],4:["d", "e"]}`. – Harshit Singh May 06 '22 at 15:46
  • @HarshitSingh: I think that difference could probably be accounted for fairly easily (although I agree not the way AKX suggested). – martineau May 06 '22 at 15:51
  • @martineau yeah coz, accessing `{1:["a"],2:["b"],3:["c"],4:["d", "e"]}` would be very easy in comparison to the output he wants, as he would not have to use `if statements` to check if its a list or not – Harshit Singh May 06 '22 at 15:54
  • @AKX I request to open the question because if he anyways want this output `{1:"a",2:"b",3:"c",4:["d", "e"]}` __only__ . He has the right to know it. – Harshit Singh May 06 '22 at 15:57
  • @HarshitSingh: Making the values always be lists is what Martijn Pieters was saying in [his comment](https://stackoverflow.com/questions/72143288/how-to-reverse-the-key-value-pairs-of-a-dictionary?noredirect=1#comment127468999_72143288) early on. If the OP wants it differently then they should be able to figure it themselves from the existing answers (as opposed to getting someone else to do it for them). – martineau May 06 '22 at 15:57

0 Answers0