Merging a list of lists

Don't use sum(), it is slow for joining lists.

Instead a nested list comprehension will work:

>>> x = [['A', 'B', 'C'], ['D', 'E', 'F'], ['G', 'H', 'I']]
>>> [elem for sublist in x for elem in sublist]
['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I']
>>> ['<tr>' + elem + '</tr>' for elem in _]

The advice to use itertools.chain was also good.


To concatenate the lists, you can use sum

values = sum([['A', 'B', 'C'], ['D', 'E', 'F'], ['G', 'H', 'I']], [])

To add the HTML tags, you can use a list comprehension.

html_values = ['<tr>' + i + '</tr>' for i in values]

import itertools

print [('<tr>%s</tr>' % x) for x in itertools.chain.from_iterable(l)]

You can use sum, but I think that is kinda ugly because you have to pass the [] parameter. As Raymond points out, it will also be expensive. So don't use sum.