Similar to this question, but instead of replacing one item with another, I\'d like to replace any occurrences of one item with the contents of a list.
orig
Different approach: when I'm doing replacements, I prefer to think in terms of dictionaries. So I'd do something like
>>> orig = [ 'a', 'b', 'c', 'd' ]
>>> rep = {'c': ['x', 'y', 'z']}
>>> [i for c in orig for i in rep.get(c, [c])]
['a', 'b', 'x', 'y', 'z', 'd']
where the last line is the standard flattening idiom.
One advantage (disadvantage?) of this approach is that it'll handle multiple occurrences of 'c'.
[update:]
Or, if you prefer:
>>> from itertools import chain
>>> list(chain.from_iterable(rep.get(c, [c]) for c in orig))
['a', 'b', 'x', 'y', 'z', 'd']
On the revised test case:
>>> orig = [ 'a', 'b', 'c', 'd', 'c' ]
>>> rep = {'c': ['x', 'y', 'z']}
>>> list(chain.from_iterable(rep.get(c, [c]) for c in orig))
['a', 'b', 'x', 'y', 'z', 'd', 'x', 'y', 'z']