Actual what I’m trying to perform, I working to with big dictionary data and variables . I need to compare the variables and dict and remove the which as relate to same. Eg:-
My words = (“ken”,col”,”PVS”)
test_dict = {"Pant": 22, "Shirt": 21, "Col high": 21, "Ken-super": 21, "Ken": 82}
print("The dictionary before performing remove is : n" + str(test_dict))
a_dict = {key: test_dict[key] for key in test_dict if key != 'Ken'}
print("The dictionary after performing remove is : n", a_dict)
If i run above code it will remove only ken only from test_dict. I need to remove the words which as contain on my words variable and also i need to remove the similar to my words variable. Like need to remove Ken-super,col High too
I need output like
{‘Pant’: 22, ‘Shirt’ : 21} only.
That’s it
My expecting the output of Remove the similar words which compare to my dict
Hekna is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
This should get you what you want.
bad_words = ("ken","col","PVS")
test_dict = {"Pant": 22, "Shirt": 21, "Col high": 21, "Ken-super": 21, "Ken": 82}
new_dict = {key: value for key, value in test_dict.items() if not any(word in key.lower() for word in bad_words)}
print(new_dict)
Kaleb Fenley is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1