×

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]

Related Topics

How to Print Pattern in Python

How to Print Pattern in Python. Pattern programs are really important for beginners and they are always asked in all the technical interviews. Multiple loops approach is used to print the different...

6 minutes read.

Scrimba python

Scrimba allows you to study whenever and wherever the topics or concepts you want. It also replaces classroom instruction with interactive screencasts, live events, and student-to-student help. Scrimba is an interactive...

3 minutes read.

What does the if __name__ == "__main__" do in Python

In this article, you will learn about the If__name__==__main__ is a statement in python to define modules and the names of the modules. This statement plays a vital role in...

3 minutes read.

Python Program to find the length of a string

Python program to find the length of a string A string in Python is a set of Unicode characters. It can't be changed once it's been declared. The number of characters...

2 minutes read.

Python Parse Text File

We will learn different ways of read text records in Python. TL;DR The accompanying tells the best way to read all texts from the readme.txt document into a string: with open('readme.txt') as f: lines...

5 minutes read.

Python coding platform

Python is a popular general-purpose programming language with many applications. High-level data structures, datatypes, dynamic binding, and many other features make it useful for both designing complex applications and "glue...

6 minutes read.

os.stat() method in Python

In Python, the os module provides the capacity to interact with the operating system. The operating system comes under the Python module. In this module, Python provides a specific feature...

3 minutes read.

How to check version of Python

How to check version of python The versions of Python come with different kinds of features and functionalities. It is not a herculean task to keep track of the updates these...

3 minutes read.

Nested for Loop in Python

"Loop" is one of the foundation concepts to learn programming in any language. Nested loops are significant steps in solving many types of problems, ranging from basic to complex scenarios....

9 minutes read.

Hypothesis Testing in Python

Hypothesis Testing in python is widely used along with statistics. Many libraries in Python are very useful for statistics and machine learning. Libraries like numpy, scripy etc. help in hypothesis testing in...

12 minutes read.

Python Comments

In this tutorial, we will discuss the importance of comments in the Python code and how various types of comments can be inserted in the code. Comments are used to describe...

3 minutes read.

Working with files in Python

Python is an object-oriented high-level programming language. Python has dynamic semantics and has high-level built-in data structures which support dynamic typing and dynamic binding. Python provides rapid development. It has an...

5 minutes read.

Python String isupper() method

Python String isupper() method The String.isupper() method in Python returns a boolean value ‘true’ if all cased characters in the string are uppercase else for any other value is returns false. Syntax String.isupper() Parameter NA Return This...

2 minutes read.

Python program to find the greatest among two numbers

Python program to find the greatest among two numbers We have many approaches to get the largest number among the two numbers, and here we will discuss a few of them....

2 minutes read.

Python Set intersection() method

Python Set intersection() method The set.intersection() method in Python returns a new set with elements that are similar between two or more sets. Syntax set.intersection(set1, set2 ... etc) Parameter set1- This parameter represents the set to search for...

2 minutes read.

Python variance() function

Variance The variance is the average of the square deviations from the mean. The variance will measure the spread of the dataset from its mean or median value. The greater the...

4 minutes read.

Check if a String is Empty in Python

The string is one of the data types of Python. This data type stores all kinds of data but all the characters must be enclosed using double quotes (""). In...

5 minutes read.

Difference between Sort and Sorted in Python

If you new to Python, it must be confusing the distinction between the Sort and Sorted functions. However, it is important to understand the differences in order to use them...

5 minutes read.

List Index out of Range Python for Loop

The List is generally used in Python to store multiple items or elements inside one single variable. The List is ordered in nature, the List can contain the elements inside...

3 minutes read.

Python RegEx

Python RegEx (python regular expressions) is a concept of writing and finding expressions in a pool of characters easily. A Regular expression (RegEx) is a set of characters that defines search...

10 minutes read.