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

后端 未结 5 632
無奈伤痛
無奈伤痛 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:44

    Clean and professional solution:

    list = [1, 2, 3]
    list1 = [3, 4, 5]
    result = list(set().union(list, list1)) # result [1, 2, 3, 4, 5]
    
    0 讨论(0)
  • 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]
    
    0 讨论(0)
  • 2021-02-04 08:00

    A slightly more efficient way to do it:

    >>> first = [1, 2, 3, 4]
    >>> second = [3, 2, 5, 6, 7]
    
    # New way
    >>> list(set(first + second))
    [1, 2, 3, 4, 5, 6, 7]
    #1000000 loops, best of 3: 1.42 µs per loop
    
    # Old way
    >>> list(set(first) | set(second))
    [1, 2, 3, 4, 5, 6, 7]
    #1000000 loops, best of 3: 1.83 µs per loop
    

    The new way is more efficient because it has only one set() instead of 2.

    0 讨论(0)
  • 2021-02-04 08:04

    If someone want to do it without set():

    a = [1,2,3]
    b = [2,3,4]
    newlist=[]
    for i in a:
        newlist.append(i)
    for z in b:
        if z not in newlist:
            newlist.append(z)
    newlist.sort()
    print newlist
    
    0 讨论(0)
  • 2021-02-04 08:07
    >>> l1 = range(10)
    >>> l2 = range(5, 15)
    >>> set(l1) | set(l2)
    set([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14])
    
    0 讨论(0)
提交回复
热议问题