Python merge all child list into parent list and remove duplicates

后端 未结 3 497
执笔经年
执笔经年 2021-01-16 23:10

I have a huge list of lists. I want to Merge all child lists to parent list and remove duplicates item from parent list after merge.

What is the optimized wa

3条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-16 23:34

    Use set

    x = [['a', 'b', 'c', 2, 4], ['x', 1, 2, 3, 'z'], ['z', 'b', 'y', 'a' 'x']]
    >>> list(set([item for sublist in x for item in sublist]))
    [1, 2, 3, 4, 'z', 'ax', 'a', 'b', 'c', 'x', 'y']
    

提交回复
热议问题