# Python – Find the difference of the sum of list elements that are missing from Matrix and vice versa

• Difficulty Level : Easy
• Last Updated : 18 Jul, 2021

Given a list and a Matrix, the task is to write a Python program that can find the difference of the sum of list elements that are missing from Matrix and vice versa.

A simpler explanation says, find and sum all elements found in Matrix but not in list. Next, find and sum all elements found in list which are not in matrix. Perform difference between sum values extracted.

Input : test_list = [[2, 4, 1], [8, 1, 2], [9, 1, 10], [4, 3, 2]], tar_list = [2, 3, 10, 7, 5, 4]
Output : 8
Explanation : 8 + 9 + 1 + 1 + 1 = 20 [ Elements in Matrix, not in list ]
7 + 5 = 12 [ Elements in list, not in Matrix ]
Difference = 12 – 20 = 8.

Input : test_list = [[2], [8], [9], [4]], tar_list = [2, 3, 10, 7, 5, 4]
Output : 8
Explanation : 8 + 9 = 17 [ Elements in Matrix, not in list ]
3 + 10 + 7 + 5 = 25 [ Elements in list, not in Matrix ]
Difference = 25 – 17 = 8.

Method 1 : Using loop and from_iterable()

In this, we get the list sum of elements present in the list and not the matrix and vice versa using a counter in loop and compute the difference. The from_iterable() is used to flatten matrix.

Example:

## Python3

 # Python3 code to demonstrate working of# Missing elements difference from Matrix and List# Using loopfrom itertools import chain  # initializing listtest_list = [[2, 4, 1], [8, 1, 2], [9, 1, 10], [4, 3, 2]]  # printing original listprint("The original list is : " + str(test_list))  # initializing target listtar_list = [2, 3, 10, 7, 5, 4]  # flattening Matrixflat_mat = list(chain.from_iterable(test_list))  # getting sum of list elements not in Matrixlist_sum = 0for ele in tar_list:    if ele not in flat_mat:        list_sum += ele  # getting sum of Matrix elements not in listmat_sum = 0for ele in flat_mat:    if ele not in tar_list:        mat_sum += ele  # computing differenceres = abs(mat_sum - list_sum)  # printing resultprint("The computed count : " + str(res))

Output:

The original list is : [[ 2, 4, 1], [8, 1, 2], [8, 1, 2], [9, 1, 10], [4, 3, 2]]

The computed count : 8

Method 2 : Using sum() and from_iterable()

In this, task of computing summation is done using sum(), rest all functionality is performed in same way as above method.

Example:

## Python3

 # Python3 code to demonstrate working of# Missing elements difference from Matrix and List# Using sum() + from_iterable()from itertools import chain  # initializing listtest_list = [[2, 4, 1], [8, 1, 2], [9, 1, 10], [4, 3, 2]]  # printing original listprint("The original list is : " + str(test_list))  # initializing target listtar_list = [2, 3, 10, 7, 5, 4]  # flattening Matrixflat_mat = list(chain.from_iterable(test_list))  # getting sum of list elements not in Matrixlist_sum = sum([ele for ele in tar_list if ele not in flat_mat])  # getting sum of Matrix elements not in listmat_sum = sum([ele for ele in flat_mat if ele not in tar_list])  # computing differenceres = abs(mat_sum - list_sum)  # printing resultprint("The computed count : " + str(res))

Output:

The original list is : [[2, 4, 1], [8, 1, 2], [9, 1, 10], [4, 3, 2]]

The computed count : 8

My Personal Notes arrow_drop_up