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

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]" 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" 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]" 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" = 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] `

## Shop Learn programming in R: courses

\$FREE Best Python online courses for 2022

\$FREE Best laptop for Fortnite

\$399+ Best laptop for Excel

\$ Best laptop for Solidworks

\$399+ Best laptop for Roblox

\$399+ Best computer for crypto mining

\$499+ Best laptop for Sims 4

\$

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

PythonStackOverflow

Check if one list is a subset of another in Python

PythonStackOverflow

How to specify multiple return types using type-hints

PythonStackOverflow

Printing words vertically in Python

PythonStackOverflow

Python Extract words from a given string

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

PythonStackOverflow

Python os.path.join () method

PythonStackOverflow

Flake8: Ignore specific warning for entire file

## Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries