What is the most pythonic way to exclude elements of a list that start with a specific character?
[x for x in my_list if not x.startswith('#')]
That's the most pythonic way of doing it. Any way of doing this will end up using a loop in either Python or C.
Not using a loop? There is filter
builtin:
filter(lambda s: not s.startswith('#'), somestrings)
Note that in Python 3 it returns iterable, not a list, and so you may have to wrap it with list()
.