Python | Subtract two list items if the item in the first list is large

NumPy | Python Methods and Functions | String Variables

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: A naive approach is to traverse both lists at the same time, and if the item in the first list is larger than the item in the second list, then subtract it, otherwise if the item in the first list is less than the item in the second list, then return the item only in the first list.

# Python code to subtract the first element
# the list is larger than the element in the second list
# otherwise we display the element in the first list .

  
# Initialize the input list

Input1 = [ 10 , 20 , 30 , 40 , 50 , 60 ]

Input2 = [ 60 , 50 , 40 , 30 , 20 , 10 ]

  
# Initializing the output list

Output = []

 

for i in range ( len (Input1)):

if Input1 [i] & gt; Input2 [i]:

Output.append (Input1 [i] - Input2 [i])

else :

Output.append (Input1 [i])

  

print ( "Original list are:" )

print (Input1)

print (Input2)

 

print ( "Output list is" )

print (Outp ut) 

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 2: Using zip () we subtract if the item in the first list is larger than the item in the second list, otherwise we output the item from the first list.

# Python code for subtracting the first element
# the list is bigger than the item in the second list,
# otherwise we display the item in the first list.

 
# Initialize the input list

Input1 = [ 10 , 20 , 30 , 40 , 50 , 60 ]

Input2 = [ 60 , 50 , 40 , 30 , 20 , 10 ]

  
# using zip ()

Output = [e1 - e2 if e1 & gt; e2 else e1 for (e1, e2) in zip (Input1, Input2)]

 
# Printout

print ( "Original list are:" )

print (Input1)

print (Input2)

 

print ( " Output list is " )

print (Output) 

Exit:

 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 3: Using comprehension list.

# Python code to subtract the first element
# the list is larger than the item in the second list,
# otherwise we display the item in the first list.

 
# Initialize the input list

Input1 = [ 10 , 20 , 30 , 40 , 50 , 60 ]

Input2 = [ 60 , 50 , 40 , 30 , 20 , 10 ]

 
# Initialize the output list

Output = [Input1 [i] - Input2 [i] if Input1 [i] & gt; Input2 [i]

else Input1 [i] for i in range ( len (Input1))]

 
# Printout

print ( "Original list are:" )

print (Input1)

print (Input2)

 

print ( "Output 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 4: Using numpy () to accomplish the above task.

# Python code to subtract the first element
# the list is larger than the element in the second list,
# otherwise we display the first list item.

 

 

import numpy as np

 
# Initialize the input list

Inp ut1 = np.array ([ 10 , 20 , 30 , 40 , 50 , 60 ])

Input2 = np.array ([ 60 , 50 , 40 , 30 , 20 , 10 ])

 
# using NumPy

Output = np.where (Input1 & gt; = Input2, Input1 - Input2, Input1)

 
# Printout

print ( "Original list are:" )

print (Input1)

print (Input2)

 

print ( "Output list is" )

print (Output) 

Output: < / p>

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




Get Solution for free from DataCamp guru