Victor Victor - 3 months ago 16
Python Question

Python how to delete list of items with a blacklist not to delete

I have a utility to delete a list of projects, but I want to know how I can add a black list filter of projects not to delete.

This is what I have right now, I run the script a few times changing the if "Languages" in project: line to delete different projects.

def delete_projects():
projects = get_projects()
# black_list = [some list of projects that I would like to delete but don't have an exact file name (i.e. "order-*")]
for project in projects:
if "Languages" in project:
delete_project(project)


I would like to make it so I can just get the list of projects and use the black_list to check for projects with names LIKE xyz* not to delete. How can I do something like this?

Thanks!

Update: this is just my current thought of implementation. Would it be better to implement it with a regular expression and deleting the project that does not match the regular expression? I would need help with the regular expression if that is the way to go.

Answer
import re
black_list_of_regex = ['order-.*', 'normal_name']
print([project for project in projects for reg in black_list_of_regex if re.match(reg, project)])

Remember if you want to match full name you should use start/end string regexp ^, $ in your regular expressions

Hope it helps