within a dictionary, how do I remove a value from a key with multiple values? Python -
from collections import ordereddict def main(): dictionary = ordereddict() dictionary["one"] = ["hello", "blowing"] dictionary["two"] = ["frying", "goodbye"] key in dictionary: print key, dictionary[key] user_input = raw_input("remove buildings ending ing? y/n") if user_input == ("y"): print "" key in dictionary: x in dictionary[key]: if ("ing") in x or ("ing") in x: del dictionary[key][x] print "" key in dictionary: print key, dictionary[key] main()
i attempting remove item "ing" in keys within dictionary, example "blowing" key "one" , "frying" key "two".
the resulting dictionary go this:
one ['hello', 'blowing'], 2 ['frying', 'goodbye']
to this:
one ['hello'], 2 ['goodbye']
dict comprehension.
return {x : [i in dictionary[x] if not i.lower().endswith('ing')] x in dictionary}
edited replace values ending 'ing' 'removed'
return {x : [i if not i.lower().endswith('ing') else 'removed' in dictionary[x]] x in dictionary}
Comments
Post a Comment