How to take two lists and combine them excluding any duplicates?

后端 未结 5 703
無奈伤痛
無奈伤痛 2021-02-04 07:26

I\'d like to make one list from two separate lists of unique items.

There are other similar questions but there didn\'t seem to be any that concerned doing this problem

5条回答
  •  名媛妹妹
    2021-02-04 07:58

    Use a set.

    >>> first = [1, 2, 3, 4]
    >>> second = [3, 2, 5, 6, 7]
    >>> third = list(set(first) | set(second))      # '|' is union
    >>> third
    [1, 2, 3, 4, 5, 6, 7]
    

提交回复
热议问题