List Subtract in Python
The List is one of the most unique Data Structures in Python. It is generally used to store multiple values in just one single variable. It is one of the most useful built-in data types in Python among the four. The remaining are Set, Dictionary and the Tuple.
This tutorial will focus on Introducing some of the basic concepts of a List in Python, like its introduction, creating a Fine List, Accessing the elements inside the List in Python using various methods like indexing, the iterative method that is looping over the List with the help of For loop. Most importantly, we will see how we can subtract the List in Python.
Creating a List in Python
The List in Python can be created using a variable name suitable to the type of elements stored in the List, and the variable will be defined with equal to, and the items or elements can be stored using the square braces [ ]. One new to the Python List concept may expect that the output just prints the elements inside the List, but that is not the case. The Compiler prints the total elements or, if accessed only by selecting a particular element, prints along with the square braces in all the circumstances in which commas separate the items or elements.
See the codes below for creating of a List in Python
Code 1:
Countries_in_the_world = ['India', 'USA', 'Russia', 'China', 'Bangladesh']
print(Countries_in_the_world)
Output
['India', 'USA', 'Russia', 'China', 'Bangladesh']
Code 2:
Famous_cities_in_India = ['Noida', 'New Delhi', 'Mumbai', 'Chennai', 'Hyderabad']
print(Famous_cities_in_India)
Output
['Noida', 'New Delhi', 'Mumbai', 'Chennai', 'Hyderabad']
Accessing the Elements inside the List
The elements inside the List can be accessed using various methods, and also we can customize the elements or items inside the List that we want to access. We firstly try to access all the elements inside the List in one go.
See the code below to see accessing all the elements inside the List at once
Code
Famous_kings_of_India = ['Akbar The Great', 'Chatrapathi Shivaji', 'Chandra Gupta Maurya']
print(Famous_kings_of_India)
Output
['Akbar The Great', 'Chatrapathi Shivaji', 'Chandra Gupta Maurya']
We will now try to access the elements inside the List using the indexing method to access the individual elements or items inside the List.
See the code below as an example
Code
Famous_kings_of_India = ['Akbar The Great', 'Chatrapathi Shivaji', 'Chandra Gupta Maurya']
print(Famous_kings_of_India[0])
print(Famous_kings_of_India[2])
print(Famous_kings_of_India[1])
Output
Akbar The Great
Chandra Gupta Maurya
Chatrapathi Shivaji
Now we will try to access the elements using the concept of indexing and also using the iterative method, that is, by using the for loop where we can customize to print 1 or 2 or even all the elements as per our choice.
See the code below for reference.
Code 1:
Famous_kings_of_India = ['Akbar The Great', 'Chatrapathi Shivaji', 'Chandra Gupta Maurya']
for i in Famous_kings_of_India:
print(i)
Output
Akbar The Great
Chatrapathi Shivaji
Chandra Gupta Maurya
Code 2:
Famous_kings_of_India = ['Akbar The Great', 'Chatrapathi Shivaji', 'Chandra Gupta Maurya']
for i in range(len(Famous_kings_of_India)):
print(Famous_kings_of_India[i])
Output
Akbar The Great
Chatrapathi Shivaji
Chandra Gupta Maurya
List Subtract in Python
List Subtraction in Python is one of the most valuable and unique features any Programming Language can offer. Differences between two Lists or Subtraction between two Lists can be made in mainly two methods one is by using the help of a set method, and the other is by not using the set method.
See the code below to find the subtraction between two Lists using the set() method.
Code
def Diff(number_List_1,number_List_2):
return list(set(number_List_1) - set(number_List_2)) + list(set(number_List_2) - set(number_List_1))
# Driver Code
number_List_1 = [10,20,30,40,50,60,70,80,90,100]
number_List_2= [100,200,300]
print(Diff(number_List_1,number_List_2))
Output
[70, 40, 10, 80, 50, 20, 90, 60, 30, 200, 300]
In the above case, we have converted the List explicitly to set and then calculated the difference and returned the value using the function. We will see how to calculate the difference between two Lists without the help of a set(). Here we will use the zip function method to perform the subtraction.
See the code below for example
Code
list1 = [90,111,32,77]
list2 = [1234,42,543,666]
result = []
for i, j in zip(list1,list2):
result.append(i - j)
print(result)
Output
[-1144, 69, -511, -589]
Performing the Subtraction of Lists using the List Comprehension method
Code
list1 = [1000, 411, 412]
list2 = [13333, 2763, 3323]
subtracted_result = [element1 - element2 for (element1, element2) in zip(list1, list2)]
print(subtracted_result)
Output
[-12333, -2352, -2911]