Home > OS >  Delete multiple dictionary in a list. If the given list of string char found in dict
Delete multiple dictionary in a list. If the given list of string char found in dict

Time:12-29

I have been trying to delete multiple dictionary in a list. If given list of string character found in dict key of value. We need to delete those dictionaries from list.

Below is the main code I am working on. Records is the list of dictionaries. As well as list of string keywords.

input_dict = [{'Key': 2296.0, 'Folder': '/Feature Annotated Image Localization/Amazon DE - Feature Image Localization/',
  'Parent task': 'Amazon DE  - 1019295902: Feature / Annotated Image Localization', 'Default task workflow': ''},
 {'Key': 2299.0, 'Folder': '/Feature Annotated Image Localization/Amazon DE',
  'Parent task': 'Amazon DE - 1025250536: Feature / Annotated Image Localization', 'Default task workflow': ''},
 {'Key': 2300.0, 'Folder': '/Feature Annotated Image Localization/Amazon DE - Feature Image Localization/',
  'Parent task': 'Amazon DE - 1025250536: Feature / Annotated Image Localization', 'Default task workflow': ''}
,{'Key': 1741.0, 'Folder': '/BBY US RDX/', 'Parent task': 'Best Buy US RDX - 999936599: Syndicated Content Request',
  'Default task workflow': '', 'Default project workflow': ''}]
for data_dict in input_dict :
    for key, value in data_dict.items():
        if key == "Folder": # Only need to check if given string charater found in Folder Key
            if value != "":
                keywords = ["RDX","Feature image","Spec sheet"]
                if any(keyword in value for keyword in keywords): 
                    input_dict.remove(data_dict)

# print(input_dict)

Expected_Output = [{'Key': 2299.0, 'Folder': '/Feature Annotated Image Localization/Amazon DE',
  'Parent task': 'Amazon DE - 1025250536: Feature / Annotated Image Localization', 'Default task workflow': ''}]

Thanks for your response.

CodePudding user response:

Never try to modify a list (sequence) while enumerating it. Better, in this case, to create a new list. Something like this:

input_dict = [{'Key': 2296.0, 'Folder': '/Feature Annotated Image Localization/Amazon DE - Feature Image Localization/',
               'Parent task': 'Amazon DE  - 1019295902: Feature / Annotated Image Localization', 'Default task workflow': ''},
              {'Key': 2299.0, 'Folder': '/Feature Annotated Image Localization/Amazon DE',
               'Parent task': 'Amazon DE - 1025250536: Feature / Annotated Image Localization', 'Default task workflow': ''},
              {'Key': 2300.0, 'Folder': '/Feature Annotated Image Localization/Amazon DE - Feature Image Localization/',
               'Parent task': 'Amazon DE - 1025250536: Feature / Annotated Image Localization', 'Default task workflow': ''},
               {'Key': 1741.0, 'Folder': '/BBY US RDX/', 'Parent task': 'Best Buy US RDX - 999936599: Syndicated Content Request','Default task workflow': '', 'Default project workflow': ''}]

out_list = []
keywords = ['RDX', 'Feature Image', 'Spec sheet']

for d in input_dict:
    if (v := d.get('Folder')) is not None:
        if not any(kw in v for kw in keywords):
            out_list.append(d)

print(out_list)

Output:

[{'Key': 2299.0, 'Folder': '/Feature Annotated Image Localization/Amazon DE', 'Parent task': 'Amazon DE - 1025250536: Feature / Annotated Image Localization', 'Default task workflow': ''}]

CodePudding user response:

Use strip().lower() in if condition.

  • Related