Open In App

Subtract two list elements if element in first list is greater – Python

Last Updated : 01 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

We are given two lists of equal length. Our task is to subtract the corresponding elements of these lists, but only if the element from the first list is greater than the corresponding element from the second list. If the element from the first list is smaller or equal, we leave the result as zero or simply skip it. For example, given two lists a = [5, 8, 12, 3] and b = [3, 9, 10, 4], we want to subtract the elements in a from b only if the element in a is greater. Let’s discuss various methods to do this.

Using List Comprehension

This method allows us to iterate through both lists, comparing the elements and subtracting them only when the element in the first list is greater.

Python
a = [5, 8, 12, 3]
b = [3, 9, 10, 4]

# Subtract elements from a if greater than corresponding elements in b
res = [x - y if x > y else 0 for x, y in zip(a, b)]
print(res)

Output
[2, 0, 2, 0]

Explanation:

  • We use list comprehension combined with the zip() function to iterate through both lists simultaneously.
  • If an element from ‘a’ is greater than the corresponding element in ‘b’, we subtract the values; otherwise, we append 0.

Let’s explore some more ways to subtract two list elements if element in first list is greater than the other.

Using for Loop

A for loop is used to iterate through both lists and apply the subtraction condition manually.

Python
a = [5, 8, 12, 3]
b = [3, 9, 10, 4]

# Initialize empty result list
res = []

# Iterate through both lists
for x, y in zip(a, b):
    if x > y:
        res.append(x - y)
    else:
        res.append(0)
print(res)

Output
[2, 0, 2, 0]

Explanation:

  • We iterate through both lists using zip().
  • For each pair of elements, we check if the element from ‘a’ is greater than the one from ‘b’ and subtract accordingly.

Using map() and lambda

We can also use the map() function along with a lambda function to achieve the same result in a functional programming style.

Python
a = [5, 8, 12, 3]
b = [3, 9, 10, 4]

# Subtract elements from a if greater than corresponding elements in b
res = list(map(lambda x, y: x - y if x > y else 0, a, b))
print(res)

Output
[2, 0, 2, 0]

Explanation:

  • map() function applies the lambda function to each pair of elements from ‘a’ and ‘b’.
  • lambda function checks if an element from a is greater and performs the subtraction, otherwise appends 0.

Using itertools.starmap()

itertools.starmap() can be used to apply a function to each element from the zipped lists.

Python
import itertools
a = [5, 8, 12, 3]
b = [3, 9, 10, 4]

# Subtract elements from a if greater than corresponding elements in b
res = list(itertools.starmap(lambda x, y: x - y if x > y else 0, zip(a, b)))
print(res)

Output
[2, 0, 2, 0]

Explanation: We use itertools.starmap() to apply the lambda function to each pair of elements in the lists.



Next Article
Practice Tags :

Similar Reads