0

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']]
Dev
  • 576
  • 3
  • 14
  • Does this answer your question? [Pythonic way to create union of all values contained in multiple lists](https://stackoverflow.com/questions/2151517/pythonic-way-to-create-union-of-all-values-contained-in-multiple-lists) – TheFungusAmongUs May 16 '22 at 04:05

3 Answers3

3

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))
j1-lee
  • 13,764
  • 3
  • 14
  • 26
1

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']]
    >>> 

0

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)

Whatever method you choose, this is the sort of thing that cries out for a function/method to do the heavy lifting, since it's quite likely you may want to do this more than once in your code (sometimes, it's worth a separate method even if only done once, as that can aid readability).

For example, these are the equivalent methods for the two options shown above, able to handle any number of input lists:

# Call with x = join_lists_uniq_any_order([list1, list2, ...])

def join_lists_uniq_any_order(list_of_lists):
    # Process each list, adding each item to (initially empty) set.

    new_set = set()
    for one_list in list_of_lists:
        for item in one_list:
            set.add(item)

    return list(set)

# Call with x = join_lists_uniq_keep_order([list1, list2, ...])

def join_lists_uniq_keep_order(list_of_lists):
    dupe_set = set()
    final_list = []

    # Process each list, adding each item if not yet seen.

    for one_list in list_of_lists:
        for item in one_list:
            if item not in dupe_set:
                set.add(item)
                final_list.append(item)
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953