del tuple-keys from nested dict; avoid runtime and type error

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



del tuple-keys from nested dict; avoid runtime and type error



I have generated a large, nested dictionary from a pandas dataframe that was originally from an .xlsx sheet. Through the process, some of the empty cells from my excel sheet were imported as 'nan' and the values are stored in tuples, which serve as keys. The resulting dictionary looks something like this:


d_nans = 'feature1': ('1', '2'): ['item1':'value1', 'item2': 'value2'],
('nan', 'nan'): ['item3':'value3', 'item4': 'value4'],
'feature2': ('3', '10'): ['item5':'value5', 'item6': 'value6'],
('nan', 'nan'): ['item7':'value7', 'item8': 'value8'],
('23', '40'): ['item9':'value9', 'item10': 'value10'],
'feature3': ('21', '5000'): ['item51':'value51', 'item61': 'value61'],
('nan', 'nan'): ['item71':'value71', 'item81': 'value81'],
('560', '2400'): ['item19':'value19', 'item110': 'value110']



I need a way to edit the dict to remove all key:value pairs where the keys have the value ('nan', 'nan'). I tried this:


for key, value in d_nans.items():
seq_id = key
feature_type = value
for key, value in feature_type.items():
if type(key) == tuple:
if key[0] == 'nan':
del feature_type[key]



Runtime Error: 'dictionary changed size over iteration'



which I tried to solve using some code from How to solve dictionary changed size during iteration in Python


for key, value in d_nans.items():
seq_id = key
feature_type = value
for key, value in feature_type.items():
for sub_key in list(feature_type.keys()):
if sub_key[0] == 'nan':
del dict[sub_key]



but this gives me


TypeError: 'type' object does not support item deletion



Any suggestions would be appreciated!




2 Answers
2



You can use dict.pop to remove specific keys if they exist. Specify a default value to avoid KeyError if the key does not exist. Since you have a nested dictionary, you can use a for loop.


dict.pop


KeyError


for


for v in d_nans.values():
v.pop(('nan', 'nan'), None)



It's not normally a good idea to modify a dictionary as you iterate over it, but you get away with it here because you aren't deleting / adding keys in the parent d_nans dictionary.


d_nans



Now let's look at your two attempts:



Iterating views while adding or deleting entries in the dictionary may
raise a RuntimeError or fail to iterate over all entries.


RuntimeError


dict


del dict[key]



Instead of looping through the inner dictionaries (feature_type) to see if ('nan', 'nan') exists, you can just check if feature_type contains ('nan', 'nan')'. This way, you can avoid both errors. Try this code:


feature_type


('nan', 'nan')


feature_type


('nan', 'nan')'


for _, feature_type in d_nans.items():
if ('nan', 'nan') in feature_type:
del feature_type[('nan', 'nan')]



If you input your example into this loop, d_nans will become the following:


d_nans


'feature1': ('1', '2'): ['item1': 'value1', 'item2': 'value2'],
'feature2': ('3', '10'): ['item5': 'value5', 'item6': 'value6'], ('23', '40'): ['item9': 'value9', 'item10': 'value10'],
'feature3': ('21', '5000'): ['item51': 'value51', 'item61': 'value61'], ('560', '2400'): ['item19': 'value19', 'item110': 'value110']






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard