python - More Pythonic way to turn any number of different lists or tuples into one list -
this question has answer here:
i trying write function take in number of different lists or tuples arguments , return 1 big list.
def addify(*args): big_list = list() iterable in args: if isinstance(iterable, tuple): big_list.extend(list(iterable)) else: big_list.extend(iterable) return big_list >>> print addify((1,2,3), [2, 5, 3], (3,1,3), [3, 2344, 3]) [1, 2, 3, 2, 5, 3, 3, 1, 3, 3, 2344, 3]
i learning args , kwargs, , code working right, seems code simple.
there must better way writing long function check if argument tuple , if add convert list , add on. seems bloated.
itertools.chain
looking for:
>>> itertools import chain >>> print list(chain((1,2,3), [2, 5, 3], (3,1,3), [3, 2344, 3])) [1, 2, 3, 2, 5, 3, 3, 1, 3, 3, 2344, 3]
note: calling list
necessary if want list instead of itertools.chain
object.
Comments
Post a Comment