Select unique value in many lists in python -
i'd select unique value in many lists, don't know how that:
a = [1,2,3,4,5] b = [2,3,4,5,6] c = [5,6,7,8,9]
i'd make 1 new list [1,2,3,4,5,6,7,8,9]
i know using following done, searching quicker way that.
for in (a, b, c): j in eachvalueineachlist: newlist.append(j) list(set(newlist)
by way, there thousand of lists in real program.
thank much.
>>> = [1,2,3,4,5] >>> b = [2,3,4,5,6] >>> c = [5,6,7,8,9] >>> list(set(a + b + c)) [1, 2, 3, 4, 5, 6, 7, 8, 9]
to avoid creating temporary list, use itertools.chain
:
>>> import itertools >>> list(set(itertools.chain(a, b, c))) [1, 2, 3, 4, 5, 6, 7, 8, 9]
update (answer comment)
if have list of lists, use itertools.chain.from_iterable
:
list(set(itertools.chain.from_iterable(a_list_of_lists)))
Comments
Post a Comment