KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I need to find out whether a name starts with any of a list's prefixes and then remove it, like: if name[:2] in ["i_", "c_", "m_", "l_", "d_", "t_", "e_", "b_"]: name = name[2:] The above only works for list prefixes with a length of two. I need the same functionality for variable-length prefixes . How is it done efficiently (little code and good performance)? A for loop iterating over each prefix and then checking name.startswith(prefix) to finally slice the name according to the length of the prefix works, but it's a lot of code, probably inefficient, and "non-Pythonic". Does anybody have a nice solution?
Tags (comma-separated)
Save Edits
Cancel