I have some code that does something like:
if string in comma_delimited_string.split(','):
return True
This website says that membership testing with sets and dicts is much faster that with lists or tuples. I know doing set(comma_delimited_string.split(','))
wouldn't improve speed because a list is still being created before it's converted into a set (or at least, it appeared to slow it down when I timed it).
I was wondering then, (mostly out of curiosity than real benefit to my code), is there a way to achieve the same effect of comma_delimited_string.split(',')
but directly creating a set, instead of a list, with the intention of speeding up the above operation?
You're ignoring the fact that in order to convert anything to a set, you need to iterate it. And that iteration is exactly the same as you are already doing in order to search the original list. So there cannot be any advantage in doing this, only overhead.
Searching against a set is more efficient if you're doing it multiple times, as that allows you to amortise the cost of the conversion. But the conversion itself is always going to be a linear scan; there's no way of avoiding that.