I would use itertools.chain.from_iterable() :
import itertools def chained(sequences): return itertools.chain.from_iterable(sequences):
or, since you noted this with python-3.3, you can use the new yield from syntax (look ma, no import!):
def chained(sequences): for seq in sequences: yield from seq
which iterators return (use list() on them if you need to materialize the complete list). In most cases, you do not need a sequence of concatenation sequences; in fact, you just want to iterate over them to process and / or search for something instead.
Note that for strings you should use str.join() instead of any of the methods described in my answer or your question:
concatenated = ''.join(sequence_of_strings)
Combined to quickly and correctly handle sequences, I would use:
def chained(sequences): for seq in sequences: yield from seq def concatenate(sequences): sequences = iter(sequences) first = next(sequences) if hasattr(first, 'join'): return first + ''.join(sequences) return first + type(first)(chained(sequences))
This works for tuples, lists, and strings:
>>> concatenate(['abcd', 'efgh', 'ijkl']) 'abcdefghijkl' >>> concatenate([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) [1, 2, 3, 4, 5, 6, 7, 8, 9] >>> concatenate([(1, 2, 3), (4, 5, 6), (7, 8, 9)]) (1, 2, 3, 4, 5, 6, 7, 8, 9)
and uses the faster ''.join() for sequence of strings.