List subtract list python

Subtract Two Lists Python

Subtract Two Lists Python | Here we will develop a program to subtract two lists in python. We will give two lists and the python program will subtract these lists using set() and without using set(). We will also develop a python program to subtract lists element by element using built-in function zip() methods and numpy.subtract() methods.

How to subtract two lists in Python:

First take and store two lists, assume we stored them in “a”, and “b” variable, then to substract them use expression: (a – b). Example:-

a = [0, 1, 2, 3, 4, 5,6]b = [0, 2, 5]a-b = [1, 3, 4,6]

How to subtract lists element by element:

a = [10, 15, 20, 30, 40]b = [5, 8, 20, 40, 25]a-b = [5, 7, 0, -10, 15]

Python Subtraction Between Two Lists

We will take two lists while declaring the variables. Then, convert the list to set using set() function and subtract sets. Finally, the subtraction value will be displayed on the screen. The set() function creates a set object. The items in a setlist are unordered, so they will appear in random order.

# Python program to subtract two lists # take list a = [1, 2, 3, 4, 5, 6, 7, 8, 9] b = [1, 3, 4, 7, 9] # print original list print('list1 =', a) print('list2 =', b) # subtraction of list sub = list(set(a) - set(b)) # print subtraction value print('list1 - list2 =', sub)

list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9]list2 = [1, 3, 4, 7, 9]list1 – list2 = [8, 2, 5, 6]

Читайте также:  Python connect to ftp server

Subtract Two arrays

In the previous program, we used the set() function but in this program, we will subtract 2 lists without using the set() function.

# Python program to subtract two lists # take list a = [10, 20, 30, 40, 50, 60, 70, 80, 90] b = [20, 30, 60, 80] # print original list print('list1 =', a) print('list2 =', b) # subtraction of list sub = [i for i in a if not i in b or b.remove(i)] # print subtraction value print('list1 - list2 =', sub)

list1 = [10, 20, 30, 40, 50, 60, 70, 80, 90]list2 = [20, 30, 60, 80]list1 – list2 = [10, 40, 50, 70, 90]

Python Subtract Lists Element by Element

In this program, we will give two lists. Then, subtract all elements present in the list and store them in a sub variable using the For Loop. Finally, the subtraction value will be displayed on the screen.

# Python program to subtract lists element by element # take list a = [20, 25, 30, 40, 55, 15] b = [5, 12, 35, 40, 45, 28] # print original list print('list1 =', a) print('list2 =', b) # subtraction of element sub = [] for i in range(len(a)): sub.append(a[i] - b[i]) # print subtraction value print('list1 - list2 =', sub)

list1 = [20, 25, 30, 40, 55, 15]list2 = [5, 12, 35, 40, 45, 28]list1 – list2 = [15, 13, -5, 0, 10, -13]

Subtract All Elements in Array

This python program also performs the same task but with different methods. In this program, we are using a built-in function. The zip() function returns a zip object, which is an iterator of tuples where the first item in each passed iterator is paired together, and then the second item in each passed iterator are paired together, etc.

# Python program to subtract lists element by element # take list a = [20, 25, 30, 40, 55, 15] b = [10, 35, 30, 26, 67, 12] # print original list print('list1 =', a) print('list2 =', b) # subtraction of element sub = [x-y for (x, y) in zip(a, b)] # print subtraction value print('list1 - list2 =', sub)

list1 = [20, 25, 30, 40, 55, 15]list2 = [10, 35, 30, 26, 67, 12]list1 – list2 = [10, -10, 0, 14, -12, 3]

Subtract Function in Python

The numpy.subtract() function is used when we want to compute the difference of two numbers or arrays. It returns the difference of numbers.

# Python program to subtract lists element by element # importng numpy.subtract() import numpy # take list a = [10, 14, 8, 64, 54, 47] b = [10, 33, 45, 12, 54, 23] # print original list print('list1 =', a) print('list2 =', b) # subtraction of element sub = numpy.subtract(a, b) # print subtraction value print('list1 - list2 =', sub)

list1 = [10, 14, 8, 64, 54, 47]list2 = [10, 33, 45, 12, 54, 23]list1 – list2 = [0 -19 -37 52 0 24]

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Источник

Python: Subtract Two Lists (4 Easy Ways!)

Python Subtract Two Lists Cover Image

In this tutorial, you’ll learn how to use Python to subtract two lists. You’ll learn how to do this using the popular data science library numpy , for-loops, list comprehensions, and the helpful built-in zip() function.

Being able to work with lists is an important skill for any Python developer, whether a beginner or an advanced Pythonista. Subtracting two lists is less intuitive than simply subtracting them. Because of this, check out the four different methods provided below to find easy ways to subtract two lists!

The Quick Answer: Use Numpy subtract()

Quick Answer - Python Subtract Two Lists

Use Numpy to Subtract Two Python Lists

The popular numpy library is often used for working in data science, and, as such, comes bundled with a ton of different helpful methods to manipulate numerical data. One of the primary advantages that numpy provides is the array object, which is very similar to the Python list object.

One of the methods that numpy provides is the subtract() method. The method takes two numpy array s as input and provides element-wise subtractions between the two lists.

Let’s see how we can use numpy and Python to subtract two lists:

# Subtract two lists with numpy import numpy as np list1 = [10, 11, 12] list2 = [1, 2, 3] array1 = np.array(list1) array2 = np.array(list2) subtracted_array = np.subtract(array1, array2) subtracted = list(subtracted_array) print(subtracted) # Returns: [9, 9, 9]

Let’s take a look at what we’ve done here:

  1. We loaded our two lists, list1 and list2 and converted them into numpy arrays, array1 and array2
  2. We then used the np.subtract() method to subtract the two lists
  3. Finally, since we want to return a Python list, we used the list() method

In the next section, you’ll learn how to use the zip() function to find the different between two lists element-wise.

Use Zip to Subtract Two Python Lists

The Python zip() function is a built-in utility that makes working with different iterable objects incredibly easy. What the function does is iterate over different objects, element by element. Because of this, the zip() function is the ideal candidate for finding the different between two lists element by element.

Let’s take a look at how we can use the Python zip() function to subtract two lists:

# Subtract two lists with for loops list1 = [10, 11, 12] list2 = [1, 2, 3] subtracted = list() for item1, item2 in zip(list1, list2): item = item1 - item2 subtracted.append(item) print(subtracted) # Returns: [9, 9, 9]

Let’s take a look at what we’ve done:

  1. We instantiated a new, empty list called subtracted that we’ll use to hold our subtracted values
  2. We then iterate over two items returned during each iteration of the zip() output.
  3. We assign the difference between these two items to item and append it to our subtracted list

Now, let’s take a look at how to use a for-loop to subtract two lists.

Want to learn how to use the Python zip() function to iterate over two lists? This tutorial teaches you exactly what the zip() function does and shows you some creative ways to use the function.

Python For Loops to Subtract Two Lists

Python for loops are incredibly helpful tools that let us repeat an action for a predetermined number of times. Because of this, we can loop over our two lists and find the different between their items.

Let’s see how we can use for loops to subtract lists:

# Subtract two lists with for loops list1 = [10, 11, 12] list2 = [1, 2, 3] subtracted = list() for i in range(len(list1)): item = list1[i] - list2[i] subtracted.append(item) print(subtracted) # Returns: [9, 9, 9]

Let’s take a look at what we’ve done with our for-loop method here:

  1. We created an empty list called subtracted
  2. We then loop over the range from 0 through to the length of our first list
  3. We then assign the difference between each i th item of each list to our variable item
  4. This item is then appended to our list

Want to learn more about Python for-loops? Check out my in-depth tutorial that takes your from beginner to advanced for-loops user! Want to watch a video instead? Check out my YouTube tutorial here.

Python List Comprehensions to Subtract Two Lists

Finally, let’s learn how we can use a list comprehension to subtract two lists. This method actually also uses the zip() function to create a new list that subtracts the two lists.

Let’s take a look at how we can do this!

# Subtract two lists with zip() list1 = [10, 11, 12] list2 = [1, 2, 3] subtracted = [element1 - element2 for (element1, element2) in zip(list1, list2)] print(subtracted) # Returns: [9, 9, 9]

Let’s take a look at what we’ve done here:

  1. We used a list comprehension to loop over the items in the zip() object of list1 and list2
  2. In this comprehension, we find the difference between the two elements

Want to learn more about Python list comprehensions? Check out this in-depth tutorial that covers off everything you need to know, with hands-on examples. More of a visual learner, check out my YouTube tutorial here.

Conclusion

In this post, you learned how to use to subtract two lists. You learned how to do this using the numpy.subract() method, the zip() function, a Python for-loop, and a Python list comprehension. Working with lists is an important skill that doesn’t always work as you’d expect, so being prepared for different challenges you might encounter is important.

To learn more about the numpy.subtract() method, check out the official documentation.

Источник

How to subtract two lists in python [duplicate]

and the result should be a new list that substracts List2 from List1, List3=[0,-2,4] ! I know, that I somehow have to use the zip-function. By doing that I get: ([(3,3), (5,7), (6,2)]) , but I don’t know what to do now?

5 Answers 5

[x1 - x2 for (x1, x2) in zip(List1, List2)] 

This uses zip , list comprehensions, and destructuring.

I like this way to do it.. I just can’t get it to work (I haven’t been working in python for a very long time)! I did this: def differences(xs,ys): [x1-x2 for (x1,x2) in zip(xs,ys)]?

This solution uses numpy. It makes sense only for largish lists as there is some overhead in instantiate the numpy arrays. OTOH, for anything but short lists, this will be blazingly fast.

>>> import numpy as np >>> a = [3,5,6] >>> b = [3,7,2] >>> list(np.array(a) - np.array(b)) [0, -2, 4] 

You can use list comprehension, as @Matt suggested. you can also use itertools — more specifically, the imap() function:

>>> from itertools import imap >>> from operator import sub >>> a = [3,5,6] >>> b = [3,7,2] >>> imap(int.__sub__, a, b) >>> for i in imap(int.__sub__, a, b): . print i . 0 -2 4 

Like all itertools funcitons, imap() returns an iterator. You can generate a list passing it as a parameter for the list() constructor:

>>> list(imap(int.__sub__, a, b)) [0, -2, 4] >>> list(imap(lambda m, n: m-n, a, b)) # Using lambda [0, -2, 4] 

EDIT: As suggested by @Cat below, it would be better to use the operator.sub() function with imap() :

>>> from operator import sub >>> list(imap(sub, a, b)) [0, -2, 4] 

Источник

Оцените статью