0

I have two dictionaries: One is :

data_obt={'Sim_1':{'sig1':[1,2,3],'sig2':[4,5,6]},'Sim_2':{'sig3':[7,8,9],'sig4':[10,11,12]},'Com_1':{'sig5':[13,14,15],'sig6':[16,17,18]},'Com_2':{'sig7':[19,20,21],'sig9':[128,23,24]}}

Other one is:

simdict={'sig1':'Bit 1','sig2':'Bit 2','sig3':'Bit 3','sig4':'Bit 4','sig5':'Bit 5','sig6':'Bit 6','sig7':'Bit 7','sig9':''}

Now I have to do return_data[fpath].append(data_obt[key][item]), where fpath = 'sig9',key='Com_2' and item = 'sig9'

But when I tried to execute this it is throwing error like : KeyError: 'sig9'

My expected return_data is {'sig9':[128,23,24]}

Can anyone please help me out?

sashkello
  • 17,306
  • 24
  • 81
  • 109
user3487900
  • 111
  • 1
  • 1
  • 6

1 Answers1

0

As I understand, return_data is another dict. If so, it doesn't (as of yet) have a key named fpath (which is 'sig9'). Hence, the error.

To avoid it, you should either use defaultdict, or initialize this element as an empty list every time you come across a new key.

Community
  • 1
  • 1
sashkello
  • 17,306
  • 24
  • 81
  • 109