×

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

Python SQLite

SQLite It is an RDBMS (Relational Database Management System). It is an embedded, serverless, transactional SQL database engine. It is an open-source application. It is named SQLite because of its lightweight....

3 minutes read.

Python for Data Analysis

Data analysis uses various techniques to read, illustrate, manipulate and evaluate a particular data. You can have access to the data and keep the data updated regularly. You can append...

4 minutes read.

API Requests using Python

What is an API? API stands for Application Programming Interface. It is commonly known as API. It provides an environment that helps two or more computer programs to contact each other....

5 minutes read.

Curdir Python

Python Programming Language Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a...

3 minutes read.

List Comprehension in Python

Sometimes we need new lists that are completely based on previously existing lists, so to perform this operation successfully, some of the programming languages come with a syntactic construct known...

5 minutes read.

Python Tricks: The Book

A Buffet of Awesome Python Features Dan Bader is the author of the book called Python Tricks . he is the owner and editor of Real Python and one of the...

3 minutes read.

Screen Rotation app Using Tkinter in Python

Tkinter: The standard Python technique for building Graphical User Interfaces (GUIs) is Tkinter, which is included in all popular Python distributions. The only framework included in the Python standard library is...

3 minutes read.

What is Sleeping Time in Python

Did you ever postpone a Python program's execution? Usually, you want your code to execute as quickly as possible. But there are times when it is in your best interests...

3 minutes read.

Python while loop

Loops are essential in Python or any other programming language, as they help to execute a block of code repetitively. Sometimes, situations arise where you would need to use a piece of code...

2 minutes read.

Anaconda python 3.7 download for windows 10 64-bit

Before installing Anaconda, you need to first know what Anaconda actually is. Anaconda is an IDE (Integrated development environment) platform used to code and executes python programs. Anaconda works both online...

3 minutes read.

Python issubclass() Function

Python issubclass() Function The issubclass() function in Python returns a boolean value ‘True’ if the given object is a subclass of classinfo, else it returns False. Syntax issubclass(class, classinfo) Parameter class: It is a required parameter which represents a tuple...

1 minute read.

Python data science course

What is meant by Data Science? When processing raw, structured, and unstructured data utilizing various technologies, algorithms, and the scientific method, data science is a detailed study of the enormous quantity...

4 minutes read.

Python program for perfect number

Python program for perfect number Before writing any program for a given problem, we have to understand the problem for which we are creating a solution program. So, let's understand what...

2 minutes read.

Keyboard Jump Game in Python

Keyword hop (jump) game is a speed composing game that aides in further developing the composing velocity of players The object of Keyboard Jump (hop) Game Python Project is to fabricate...

7 minutes read.

Working with JSON in Python

Python is an Object-Oriented high-level language. Python is designed to be highly beginner-friendly. Python has an English-like syntax, which is very easy to read. In this article, we are going...

4 minutes read.

Paramiko Python Example

Python Programming Language Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It...

6 minutes read.

Python id() function

Python id() function The id() function in Python returns an id for the specified object where all the objects in has its own unique id. Syntax id(object) Parameter object: This parameter represents any object, String, Number, List,...

1 minute read.

Python | Read csv using pandas.read_csv()

Python is an excellent language for performing information analysis, owing to the fantastic biological system of information-driven python packages. Pandas is one of those packages that make taking in and...

4 minutes read.

Adding item to a python dictionary

The dictionary is one of python’s built-in data structures where it stores key-value pairs. A dictionary is a collection of ordered values that can be changeable. We can add, modify,...

3 minutes read.

Python List insert() method

Python List insert() method The list.insert () method in Python inserts an item at the specified position. Syntax list.insert(i, x) Parameter i: This parameter represents a number specifying the position to insert the given value. x: This parameter signifies...

1 minute read.