, itertools.groupby
chain
.
from itertools import groupby, chain
isstr = lambda x: isinstance(x, basestring)
# on Python 3: lambda x: isinstance(x, str)
rule = ["a", "b", C(), "d", "ef", "g"]
list(chain.from_iterable(
# join string groups into single-element sequence,
# otherwise just chain the group itself
(''.join(group), ) if group_isstr else group
for group_isstr, group in groupby(rule, isstr)
))
['ab', <__main__.C object at 0x108dfdad0>, 'defg']