user352258
user352258

Reputation: 45

Map a dictionary to another dictionary with combination of single values and tuples

I have the following:

# Op Codes
A_CMD = 17
B_CMD = 18
C_CMD = 19
D_CMD = 20
E_CMD = 21

cmd_dict = {
  A_CMD : "ORANGE",
  B_CMD : ("RED", "GREEN"),
  C_CMD : "BLACK",
  D_CMD : ("PURPLE", "BLUE"),
  E_CMD : "WHITE"
}

color_dict = {
  "RED"    : 1,
  "WHITE"  : 2,
  "BLUE"   : 3,
  "GREEN"  : 4,
  "ORANGE" : 5,
  "BLACK"  : 6,
  "PURPLE" : 7
}

I want to get a dictionary which maps cmd_dict to color_dict, while cmd_dict contains a combination of tuple and single string values.

CmdToColorDict = {
  A_CMD : 5,
  B_CMD : (1, 4),
  C_CMD : 6,
  D_CMD : (7, 3),
  E_CMD : 2
}

I tried : cmdToColorDict = {color_dict.get(key, key):value for key, value in cmd_dict.items()}

But it yields the original cmd_dict : {17: 'ORANGE', 18: ('RED', 'GREEN'), 19: 'BLACK', 20: ('PURPLE', 'BLUE'), 21: 'WHITE'}

EDIT: (I got it to work!)

cmdToColorDict = {}
for key in cmd_dict:
  if(type(cmd_dict[key]) is tuple):
    L=list(())
    for item in cmd_dict[key]:
      L.append(str(color_dict[item]))
    cmdToColorDict[key] = tuple(L)
  else:
       cmdToColorDict[key] = color_dict[cmd_dict[key]]

Which yields: {17: 5, 18: ('1', '4'), 19: 6, 20: ('7', '3'), 21: 2}

Is there a way I can make this code slimmer?

Upvotes: 0

Views: 57

Answers (1)

qouify
qouify

Reputation: 3900

First, you access cmd_dict[key] several times in your loop. You can iterate over cmd_dict.items() (which generates tuples of (key, value)) to avoid that:

cmdToColorDict = {}
for key, val in cmd_dict.items():
    if isinstance(val, tuple):
        L = list(())
        for item in val:
            L.append(str(color_dict[item]))
        cmdToColorDict[key] = tuple(L)
    else:
        cmdToColorDict[key] = color_dict[val]

Then the nested loop can be avoided with a generator expression:

cmdToColorDict = {}
for key, val in cmd_dict.items():
    if type(cmd_dict[key]) is tuple:
        cmdToColorDict[key] = tuple(str(color_dict[item]) for item in val)
    else:
        cmdToColorDict[key] = color_dict[val]

Finally you can use a dict comprehension to encode that loop, but it's not necessarily more readable. It's up to you.

cmdToColorDict = {
    key: tuple(str(color_dict[item]) for item in val)
    if isinstance(val, tuple)
    else color_dict[val]
    for key, val in cmd_dict.items()
}

Anyway, if you're not familiar with list or dict comprehension or generator, it's a good exercise to try to translate such loops into these.

Upvotes: 1

Related Questions