remove pairs from nested dictionary that contain a character

I have a dictionary in the following format, and I want to keep only unit items that have a negative value

dic = dict(({'a': [{'unit': 'x', 'value': '1', 'type':'int'},{'unit': '19', 'value':'-1', 'input':'man'},{'unit': 'y', 'value':'-2'}],
      'b': [{'unit':'b', 'value': '3', 'type':'int'},{'unit': 'g', 'value':'3'},{'unit': 'tol', 'value':'-12'}]}))

I have this code – where the out variable does print the intended part of the dictionary, but im having trouble appending it back to the correct key and outputting a complete dictionary. Also perhaps an inelegant way of selecting negative, values, is that I am simply selecting fields that contain a ‘-‘ in the value pair..

out=[]
for k, v in dic.items():
    for x in v:
        if '-' in x['value']:
            out=x
    dic[k].append(dict(out))

the expected dictionary would be like this

dict(({'a': [{'unit': '19', 'value':'-1', 'input':'man'},{'unit': 'y', 'value':'-2'}],
      'b': [{'unit': 'tol', 'value':'-12'}]}))

thanks for looking

Answer

If you really want negative numeric values, casting the str to int/float rather than checking for the '-' character in the string is more elegant.

>>> new_dic = {}
>>> for key, val in dic.items():
        new_dic[key] = [entry for entry in val if int(entry['value']) < 0]

>>> # or as an equivalent one-liner:
>>> new_dic = {key: [entry for entry in val if int(entry['value']) < 0] for key, val in dic.items()}

>>> from pprint import pprint
>>> pprint(new_dic)
{'a': [{'input': 'man', 'unit': '19', 'value': '-1'},
       {'unit': 'y', 'value': '-2'}],
 'b': [{'unit': 'tol', 'value': '-12'}]}

Additionally we can clean up your initial dictionary definition. There’s a whole lot of unnecessary stuff going on. There’s no need to wrap everything in extra parentheses or use the dict constructor. Simply use the {<def'n>} construct directly:

dic = {
    'a': [{'unit': 'x', 'value': '1', 'type':'int'},
          {'unit': '19', 'value':'-1', 'input':'man'},
          {'unit': 'y', 'value':'-2'}],
    'b': [{'unit':'b', 'value': '3', 'type':'int'},
          {'unit': 'g', 'value':'3'},
          {'unit': 'tol', 'value':'-12'}]
}