I’m using regex to look for matches. Is it more efficient to use it on a long string or that same string broken up into a list?
For Example
mystring = "asdfl;jkasdfj;ldj;lj;dlskjfasdfjkl; ;lj ;lf ak;lkjf al;kjdlkjsdl;fkja;dlkjf a;lsdf"
OR
mylist = ["asdfl;jkasdfj;ldj;lj;dlskjfasdfjkl;",";lj", ";lf ak;lkjf","al;kjdlkjsdl;fkja;dlkjf a;lsdf"]
The simpler and more efficient way, because of how the
remodule is written, is to use the regex on the string. This isn’t definitively faster, but it’s far more efficient because it’s easier to code, easier to maintain, and makes better use of Python’s included batteries.reis optimized for long strings, not lists. If you were to use a regex on that list of strings, you’d end up making quite a few calls to the functions inre.Basic rule, don’t make optimizations unless they’re necessary because the existing way of doing it is too slow.