Wednesday, April 24, 2024
HomePythonSubtract Two Python Lists (Component-Smart)? – Finxter

Subtract Two Python Lists (Component-Smart)? – Finxter


Drawback Formulation and Resolution Overview

This text will present you the best way to subtract two Lists in Python.

💡 Be aware: Earlier than subtracting these Lists, guarantee every Record is of the identical size and comprises subtractable knowledge. For instance, subtracting a Record of strings from a Record of integers or floats will end in an error. Whereas subtracting a Record of integers from a Record of floats, or vice-versa will work.

This text makes use of two (2) Lists, one Record containing 5 integer values and the opposite 5 float values.

For instance:

  • Enter 1: [1, 2, 3, 4, 5]
  • Enter 2: [1, 2, 3, 4, 1]
  • Output: [0, 0, 0, 0, 4]

💬 Query: How would we write code to subtract two lists?

We will accomplish this job by one of many following choices:


Technique 1: Use Record Comprehension and zip()

This instance makes use of Record Comprehension and the zip() operate to subtract two (2) Lists.

list_ints   = [2028, 3913, 2816, 4301, 1853]
list_floats = [335.36, 442.88, 200.54, 327.11, 410.09]

new_list = [round(x-y,2) for x, y in zip(list_ints, list_floats)]
print(new_list)

The above code declares two (2) Lists: a Record of integers and a Record of floats. These save to the variables list_ints and list_floats, respectively.

The following line makes use of Record Comprehension which does the next:

  • The zip() operate is known as and handed two (2) arguments: iterables. On this case, list_ints and list_floats. This operate returns a zipper object iterator (for instance: <zip object at 0x000001ED24253B80>).
  • Subsequent, every aspect from list_ints (x) is subtracted from int_floats (y) by way of the zip object proven above
  • The outcomes from this operation are rounded down to 2 (2) decimal locations.

These outcomes save to new_list and output to the terminal.

[1692.64, 3470.12, 2615.46, 3973.89, 1442.91]

💡Be aware: To make sure the outcomes have solely two (2) decimal locations, the spherical() operate was used.


Technique 2: Use np.subtract()

This instance makes use of the np.subtract() operate from the NumPy library to subtract two (2) Lists.

The NumPy library will must be put in to run this code error-free. Click on right here in case you require set up directions.

import numpy as np

list_ints = np.array([2028, 3913, 2816, 4301, 1853])
list_floats = np.array([335.36, 442.88, 200.54, 327.11, 410.09])
new_list = np.subtract(list_ints, list_floats)
print(new_list)

The primary line of the above code imports the NumPy library to make the np_array() and np.subtract() features obtainable.

The next two (2) strains create two (2) Lists, a Record of integers and a Record of floats. These Lists are transformed to a NumPy array() and saved to the variables list_ints and list_floats, respectively.

If output to the terminal, the contents of those Lists seem as follows:

[335.36 442.88 200.54 327.11 410.09]
[1692.64 3470.12 2615.46 3973.89 1442.91]

Subsequent, the np.subtract() operate is known as and handed two (2) NumPy arrays. On this case, list_ints and list_floats.

The subtraction operation is carried out (np.subtract(list_ints, list_floats)) on stated Lists, and the outcomes save to new_list and are output to the terminal.

[1692.64 3470.12 2615.46 3973.89 1442.91]

Technique 3: Use operator.sub and map()

This instance imports the operator.sub along side the map() operate to subtract two (2) Lists.

import operator

list_ints   = [2028, 3913, 2816, 4301, 1853]
list_floats = [335.36, 442.88, 200.54, 327.11, 410.09]
print(checklist(map(operator.sub, list_ints, list_floats)))

The primary line within the above code imports the operator library. This library provides alternate methods to carry out customary Python operations, resembling add, subtract, multiply and far, way more!

The next two (2) strains create two (2) Lists, a Record of integers and a Record of floats. These Lists are saved to the variables list_ints and list_floats, respectively.

The following line calls the print() operate.

Inside print(), the map() operate is known as and handed three (3) arguments: the operation to carry out (operator.sub), an iterable (list_ints), and one other iterable (list_floats) which return a map object.

If the map object was output to the terminal, an object much like under would show.

<map object at 0x0000024DEEB3B190>

The subtraction operator is then carried out, and the outcomes convert to a Record and output to the terminal.

[1692.6399999999999, 3470.12, 2615.46, 3973.89, 1442.91]

Technique 4: Use map() and a lambda

This instance makes use of a lambda along side the map() operate to subtract two (2) Lists.

list_ints   = [2028, 3913, 2816, 4301, 1853]
list_floats = [335.36, 442.88, 200.54, 327.11, 410.09]
new_list = checklist(map(lambda x, y: x-y, list_ints, list_floats))
print(new_list)

The above code declares two (2) Lists: a Record of integers and a Record of floats. These save to the variables list_ints and list_floats, respectively.

The next calls the map() operate. This operate is handed three (3) arguments, a lambda, an iterable (list_ints), and one other iterable (list_floats). The outcomes return an iterable map object.

The following line calls the print() operate.

Inside print(), the map() operate is known as and handed three (3) arguments: the operation to carry out (operator.sub), an iterable (list_ints), and one other iterable (list_floats) which return a map object.

If the map object was output to the terminal, an object much like under would show.

<map object at 0x000001CD6480BFD0>

The subtraction operator is then carried out, transformed to a Record, saved to new_lists and output to the terminal.

[1692.6399999999999, 3470.12, 2615.46, 3973.89, 1442.91]

Technique 5: Use Record Comprehension and enumerate()

This instance makes use of Record Comprehension and enumerate() operate to subtract two (2) Lists.

list_ints   = [2028, 3913, 2816, 4301, 1853]
list_floats = [335.36, 442.88, 200.54, 327.11, 410.09]
new_list = [x - list_floats[i] for i, x in enumerate(list_ints)]
print(new_list)

The above code declares two (2) Lists: a Record of integers and a Record of floats. These save to the variables list_ints and list_floats, respectively.

The next line makes use of Record Comprehension to subtract two (2) Lists. This operator makes use of the enumerate() operate and is handed one (1) argument: list_ints. The outcomes save to new_list and are output to the terminal.

💡 Be aware: The enumerate() operate takes, for instance, a Record, a Tuple, and so on., as an argument. This may then add a counter and use this as a key for the stated object.


Technique 6: Use For Loop

This instance makes use of a for loop to subtract two (2) Lists.

list_ints   = [2028, 3913, 2816, 4301, 1853]
list_floats = [335.36, 442.88, 200.54, 327.11, 410.09]
new_list = []

for i in vary(len(list_ints)):
    new_list.append(list_ints[i] - list_floats[i])
print(new_list)

The above code declares three (2) Lists: a Record of integers, a Record of floats, and an empty checklist. These save to the variables list_ints, list_floats, and new_list, respectively.

Subsequent a for loop is instantiated. This loop loops by way of every aspect of list_ints, carry out the subtraction operation and append the outcomes to new_list. The outcomes are output to the terminal.

[1692.6399999999999, 3470.12, 2615.46, 3973.89, 1442.91]

Abstract

This text has supplied 5 (5) methods to subtract two Lists to pick out the perfect match to your coding necessities.

Good Luck & Joyful Coding!


Programmer Humor – Blockchain

“Blockchains are like grappling hooks, in that it’s extraordinarily cool once you encounter an issue for which they’re the proper resolution, however it occurs method too hardly ever in actual life.” supply xkcd
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments