Home > OS >  Taking the union of 2 lists of lists in Python
Taking the union of 2 lists of lists in Python

Time:05-16

I have 2 list of lists.

One is say list1, which is :

[['1', '2', '*', '2', '1', '0.8'],
 ['1', '2', '3', '1', '1', '0.7'],
 ['*', '*', '3', '4', '1', '0.5'],
 ['1', '2', '*', '1', '1', '0.3'],
 ['2', '2', '*', '2', '1', '0.1']]

And list2 is :

[['3', '*', '1', '4', '1', '0.9'],
 ['1', '2', '2', '2', '1', '0.4'],
 ['2', '2', '*', '2', '1', '0.1']]

Now I want to get the union of these 2 list of lists and create a 3rd list of list and as ['2', '2', '*', '2', '1', '0.1'] this list is there in both of them so I want this to be in the final list of list for only 1 time.

I am doing:

final_list=list1   list2

Final list is producing :

[['3', '*', '1', '4', '1', '0.9'],
 ['1', '2', '*', '2', '1', '0.8'],
 ['1', '2', '3', '1', '1', '0.7'],
 ['*', '*', '3', '4', '1', '0.5'],
 ['1', '2', '2', '2', '1', '0.4'],
 ['1', '2', '*', '1', '1', '0.3'],
 ['2', '2', '*', '2', '1', '0.1'],
 ['2', '2', '*', '2', '1', '0.1']]

My desired outcome is :

[['3', '*', '1', '4', '1', '0.9'],
 ['1', '2', '*', '2', '1', '0.8'],
 ['1', '2', '3', '1', '1', '0.7'],
 ['*', '*', '3', '4', '1', '0.5'],
 ['1', '2', '2', '2', '1', '0.4'],
 ['1', '2', '*', '1', '1', '0.3'],
 ['2', '2', '*', '2', '1', '0.1']]

CodePudding user response:

If you don't care about order, you can convert them to sets and union (|) them:

list1 = [['1', '2', '*', '2', '1', '0.8'], ['1', '2', '3', '1', '1', '0.7'], ['*', '*', '3', '4', '1', '0.5'], ['1', '2', '*', '1', '1', '0.3'], ['2', '2', '*', '2', '1', '0.1']]
list2 = [['3', '*', '1', '4', '1', '0.9'], ['1', '2', '2', '2', '1', '0.4'], ['2', '2', '*', '2', '1', '0.1']]

output = set(map(tuple, list1)) | set(map(tuple, list2))
print(output)
# {('1', '2', '*', '1', '1', '0.3'),
#  ('2', '2', '*', '2', '1', '0.1'),
#  ('1', '2', '3', '1', '1', '0.7'),
#  ('1', '2', '*', '2', '1', '0.8'),
#  ('3', '*', '1', '4', '1', '0.9'),
#  ('1', '2', '2', '2', '1', '0.4'),
#  ('*', '*', '3', '4', '1', '0.5')}

If you want to have a list of lists (instead of a set of tuples), add the following:

output = list(map(list, output))

CodePudding user response:

While this has been answered, here's a pretty simple way of doing it creating a third list and only appending elements to the third list if the element doesn't exist yet.

    >>> results = []
    >>> for sublist in list1   list2:
    ...     if sublist not in results:
    ...         results.append(sublist)
    ... 

>>> pp(results)

    [['1', '2', '*', '2', '1', '0.8'],
     ['1', '2', '3', '1', '1', '0.7'],
     ['*', '*', '3', '4', '1', '0.5'],
     ['1', '2', '*', '1', '1', '0.3'],
     ['2', '2', '*', '2', '1', '0.1'],
     ['3', '*', '1', '4', '1', '0.9'],
     ['1', '2', '2', '2', '1', '0.4']]
    >>> 

CodePudding user response:

You should be able to create a set, add each element of each list to that set, then create a list from that set, something like:

new_set = set()
for item in list1:
    set.add(item)
for item in list2:
    set.add(item)
final_list = list(set)

It won't be guaranteed to be in order but it will have only unique elements. Of course, if you don't care about order, you may as well be using sets exclusively so you don't have to concern yourself with duplicates.


If you want to maintain order, you just maintain both a final list and the set, and only add to the final list if it's not already in the set:

dupe_set = set()
final_list = []
for item in list1:
    if item not in dupe_set:
        set.add(item)
        final_list.append(item)
for item in list1:
    if item not in dupe_set:
        set.add(item)
        final_list.append(item)
  • Related