Python: merge lists
A simple utility function to merge several lists in a single one, using the functional programming tools:
Example:
The same but removing duplicates:
The same but removing duplicates later:
Other (easier) ways to achieve the same?
def merge(*input):
return reduce(list.__add__, input, list())
>>> a = [0, 1, 2]
>>> b = [2, 3, 4]
>>> c = [4, 5, 6]
>>> merge(a, b, c)
[0, 1, 2, 2, 3, 4, 4, 5, 6]
def merge_uniq(*input):
return list(reduce(set.union, input, set()))
def merge_uniq(*input):
return list(set(merge(input)))
[ show comments ]
blog comments powered by Disqus