0

Here's a sample dictionary.

example = {'a': 10, 'b': 12, 'c': 10, 'd': 12}

I want to print them like below.

12 b, d
10 a, c
jpp
  • 159,742
  • 34
  • 281
  • 339
Lairum
  • 19
  • 3
  • [Python reverse / invert a mapping](//stackoverflow.com/q/483666) – Aran-Fey May 21 '18 at 08:38
  • Your dictionary syntax is wrong: It should be example = {'a' : 10, 'b' : 12, 'c' : 10, 'd' : 12} –  May 21 '18 at 08:38
  • Welcome to Stack Overflow. Please read [How to ask a good question](https://stackoverflow.com/help/how-to-ask) – Sven 31415 May 21 '18 at 08:41
  • @Aran-Fey thanks for the dupe target. I'm closing it with that. – Jean-François Fabre May 21 '18 at 08:46
  • It never ends. This is NOT A DUPLICATE. The OP likely wants the data printed in that format, not to be able to simply perform an operation that would lead to it. There are a million "duplicates" like this a day. It's nothing short of ridiculous and it needs to stop. It's like you all look for any way for a question to be considered a duplicate: "does this question need recursion? Duplicate!" Never mind that the objective for the "duplicate" and the original deal with entirely separate issues and seek entirely different results. For the love of all that is holy, give these guys a break, – Aaron Brandhagen May 21 '18 at 09:01
  • @AaronBrandhagen I see where you're coming from (which is why I didn't close the question as dupe), but you have to admit that this is just a trivial combination of the two questions *"How do I invert a dict?"* and *"How do I print a comma-separated list of values?"*. I've added a relevant dupe. – Aran-Fey May 21 '18 at 17:35
  • Aran. I truly appreciate your disposition in response. The learning curve in programming is unparalleled. There's nothing that even compares. Patterns don't exist. It's exactly what it's called: code. The knowledge gap between an adept coder and a beginner (especially self-taught) is literally incomprehensible. The simplest, most rudimentary piece of code to you is hundreds/thousands of hours of work removed from a beginner to even have a decent understanding of it. Programming is a galactic web of interrelated abstraction. The difficulty grows exponentially as more layers are added. ..... – Aaron Brandhagen May 21 '18 at 19:51
  • So, trivial questions to you is to the beginner yet another piece of the jigsaw - cramming yet another pattern-less concept/paradigm into memory and connecting them to the rest of material learned while constantly running into mindless, infuriating contradictions that need to be kept to memory, too. Let me know if I make any sense. I hate having conversations like these via text, so much can get lost, misunderstood. – Aaron Brandhagen May 21 '18 at 19:56
  • The point (sorry, I'm VERY passionate about this subject): the smallest of differences in code are most of the time hundreds of conditions/rules removed from each other. And those conditions and rules have cascading effect on a lot more than the single issue at hand. Patterns are learned eventually, but only after the curve is surpassed. That soul-crushing, beast of a curve. By that point? You know what you know with out any sort of map or patterns to frame what you know. So, once the basics become basic to you, it's impossible to know how a beginner is processing their own web of learning. – Aaron Brandhagen May 21 '18 at 20:11

2 Answers2

1

There are two ways you can approach this problem.

Efficient: collections.defaultdict(list)

Construct a new dictionary with keys and values inverted. Importantly, you can have duplicate values, so we use a list to hold these. The Pythonic approach is to use collections.defaultdict.

For very large dictionaries, this may have a large memory overhead.

example = {'a': 10, 'b': 12, 'c': 10, 'd': 12}

from collections import defaultdict

d = defaultdict(list)

for k, v in example.items():
    d[v].append(k)

for k, v in d.items():
    print(k, ' '.join(v))

10 a c
12 b d

Manual: loop of list comprehensions

This way is computationally inefficient, but requires less memory overhead:

for value in set(example.values()):
    print(value, ' '.join([k for k, v in example.items() if v == value]))

10 a c
12 b d
jpp
  • 159,742
  • 34
  • 281
  • 339
0

Does this achieve what you're after?

for i in set(example.values()):
    print (i, [list(example.keys())[j] for j in range(len(example)) if list(example.values())[j]==i])
ignoring_gravity
  • 6,677
  • 4
  • 32
  • 65