Friday, December 27, 2024
Google search engine
HomeLanguagesPython | Subtract two list elements if element in first list is...

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

Given two list, If element in first list in greater than element in second list, then subtract it, else return the element of first list only.
Examples:

Input:
l1 = [10, 20, 30, 40, 50, 60]
l2 = [60, 50, 40, 30, 20, 10]
Output:
[10, 20, 30, 10, 30, 50]

Input:
l1 = [15, 9, 10, 56, 23, 78, 5, 4, 9]
l2 = [9, 4, 5, 36, 47, 26, 10, 45, 87]
Output:
[6, 5, 5, 20, 23, 52, 5, 4, 9]

Method 1: The naive approach is to traverse both list simultaneously and if the element in first list in greater than element in second list, then subtract it, else if the element in first list in smaller than element in second list, then return element of first list only.
 

Python3




# Python code to subtract if element in first
# list is greater than element in second list,
# else we output element of first list.
 
# Input list initialisation
Input1 = [10, 20, 30, 40, 50, 60]
Input2 = [60, 50, 40, 30, 20, 10]
 
# Output list initialisation
Output = []
 
for i in range(len(Input1)):
    if Input1[i] > Input2[i]:
        Output.append(Input1[i] - Input2[i])
    else:
        Output.append(Input1[i])
         
print("Original list are :")
print(Input1)
print(Input2)
 
print("\nOutput list is")
print(Output)


Output:

Original list are :
[10, 20, 30, 40, 50, 60]
[60, 50, 40, 30, 20, 10]

Output list is
[10, 20, 30, 10, 30, 50]

Time Complexity: O(n*n) where n is the number of elements in the list. 
Auxiliary Space: O(n) constant additional spaceof size n is created

Method 2: Using zip() we subtract if element in first list is greater than element in second list, else we output element of first list.

Python3




# Python code to subtract if element in first
# list is greater than element in second list,
# else we output element of first list.
 
# Input list initialisation
Input1 = [10, 20, 30, 40, 50, 60]
Input2 = [60, 50, 40, 30, 20, 10]
 
# using zip()
Output =[e1-e2 if e1>e2 else e1 for (e1, e2) in zip(Input1, Input2)]
 
# Printing output
print("Original list are :")
print(Input1)
print(Input2)
 
print("\nOutput list is")
print(Output)


Output:

Original list are :
[10, 20, 30, 40, 50, 60]
[60, 50, 40, 30, 20, 10]

Output list is
[10, 20, 30, 10, 30, 50]

Time Complexity: O(n*n) where n is the number of elements in the list. 
Auxiliary Space: O(n) constant additional spaceof size n is created

Method 3: Using list comprehension 

Python3




# Python code to subtract if element in first
# list is greater than element in second list,
# else we output element of first list.
 
# Input list initialisation
Input1 = [10, 20, 30, 40, 50, 60]
Input2 = [60, 50, 40, 30, 20, 10]
 
# Output list initialisation
Output = [Input1[i]-Input2[i] if Input1[i] > Input2[i] \
          else Input1[i] for i in range(len(Input1))]
 
# Printing output
print("Original list are :")
print(Input1)
print(Input2)
 
print("\nOutput list is")
print(Output)


Output: 

Original list are :
[10, 20, 30, 40, 50, 60]
[60, 50, 40, 30, 20, 10]

Output list is
[10, 20, 30, 10, 30, 50]

Time complexity: O(n*n), where n is the length of the numbers list. The list comprehension has a time complexity of O(n)
Auxiliary Space: O(n), where n is the length of the numbers list.

Method 4: Using numpy() to complete the above task.

Python3




# Python code to subtract if element in first
# list is greater than element in second list,
# else we output element of first list.
 
  
import numpy as np
 
# Input list initialisation
Input1 = np.array([10, 20, 30, 40, 50, 60])
Input2 = np.array([60, 50, 40, 30, 20, 10])
  
# using numpy
Output = np.where(Input1 >= Input2, Input1 - Input2, Input1)
  
# Printing output
print("Original list are :")
print(Input1)
print(Input2)
 
print("\nOutput list is")
print(Output)


Output:

Original list are :
[10 20 30 40 50 60]
[60 50 40 30 20 10]

Output list is
[10 20 30 10 30 50]

Method 5: Using map() and lambda function

Python3




# Python code to subtract if element in first
# list is greater than element in second list,
# else we output element of first list.
 
Input1 = [10, 20, 30, 40, 50, 60]
Input2 = [60, 50, 40, 30, 20, 10]
 
Output = list(map(lambda x, y: x - y if x > y else x, Input1, Input2))
 
print("Original list are :")
print(Input1)
print(Input2)
 
print("\nOutput list is")
print(Output)
#This code is contributed by Edula Vinay Kumar Reddy


Output

Original list are :
[10, 20, 30, 40, 50, 60]
[60, 50, 40, 30, 20, 10]

Output list is
[10, 20, 30, 10, 30, 50]

The time complexity  is O(n) where n is the number of elements in the input lists, since we are iterating over the elements of the lists once. The space complexity is O(n) as well, since we are creating a new list of the same size as the original lists.

Method 6: Using Pandas

Python3




import pandas as pd
import numpy as np
Input1 = [10, 20, 30, 40, 50, 60]
Input2 = [60, 50, 40, 30, 20, 10]
df = pd.DataFrame({"Input1": Input1, "Input2": Input2})
df["Output"] = np.where(df["Input1"] > df["Input2"], df["Input1"] - df["Input2"], df["Input1"])
Output = df["Output"].tolist()
print("Original list are :")
print(Input1)
print(Input2)
 
print("\nOutput list is")
print(Output)


Output:

Original list are :
[10, 20, 30, 40, 50, 60]
[60, 50, 40, 30, 20, 10]

Output list is
[10, 20, 30, 10, 30, 50]

Time Complexity: O(n)
Auxiliary Space: O(n)

RELATED ARTICLES

Most Popular

Recent Comments