Python Difference Between Two Lists using Python Set and Without Set – My programming school

In this tutorial, we’ll uncover two Pythonic methods to find the python Difference Between Two Lists. One of the strategies is using the Python Set. It first converts the lists into units and then will get the distinctive half out of that.

Other non-set strategies examine two lists component by component and accumulate the distinctive ones. We can implement these by using nested for loops and with record comprehension.

By the way in which, if you’re not conscious of the units in Python, then comply with the tutorial. It would rapidly introduce you to how Python implements the mathematical type of Set.

How to Find the Python Difference Between Two Lists

Python Set appears to be the obvious option to determine the frequency as effectively as the distinction of two lists. So, we’re going to discover it first and then will use nested loops and record comprehension.

Before we transfer to the answer half, let’s outline the take a look at the parameters, i.e., the 2 lists which now we have to find the distinction.

# Test Input
list_a = [11, 16, 21, 26, 31, 36, 41]
list_b = [26, 41, 36]

And we wish our resolution to supply the next output:

# Expected Result
# list_out = list_a - list_b
list_out = [11, 21, 31, 16]

Let’s begin to create a program to find the python distinction between two lists, first using units.

Use set() to find the python distinction between two lists

In this strategy, we’ll first derive two SETs (say set1 and set2) from the LISTs (say list1 and list2) by passing them to set() perform. After that, we’ll carry out the set distinction operation. It will return these parts from the list1 which don’t exist in the second.

Here is the pattern program to get the python distinction of two lists.

"""
Desc:
 Using set() to find the python distinction between two lists in Python
"""

def list_diff(list1, list2): 
	return (record(set(list1) - set(list2))) 

# Test Input
list1 = [11, 16, 21, 26, 31, 36, 41] 
list2 = [26, 41, 36] 

# Run Test
print(list_diff(list1, list2)) 

After working on this, you need to see the next end result:

[16, 11, 21, 31]

Without set(), using nested loops

In this methodology, we’ll use a nested For Loop to check every component of the primary record with the second. And while traversing, we’ll be appending each non-matching merchandise to a new (and empty) record.

The new record would finally embody the distinction between the 2 given sequences. Below is the pattern program to reveal this strategy.

"""
Desc:
 Nested loop to find the python distinction between two lists in Python
"""

def list_diff(list1, list2):
    out = []
    for ele in list1:
        if not ele in list2:
            out.append(ele)
    return out

# Test Input
list1 = [11, 16, 21, 26, 31, 36, 41] 
list2 = [26, 41, 36] 

# Run Test
print(list_diff(list1, list2)) 

After working on the above program, you need to see the next end result:

[11, 16, 21, 31]

Without set(), using record comprehension

It is virtually an analogous approach that we used in the earlier one. Here, we changed the nested loops with the record comprehension syntax.

See the instance beneath.

"""
Desc:
 List comprehension to find the python distinction between two lists in Python
"""

def list_diff(list1, list2):
    out = [item for item in list1 if not item in list2]
    return out

# Test Input
list1 = [11, 16, 21, 26, 31, 36, 41] 
list2 = [26, 41, 36] 

# Run Test
print(list_diff(list1, list2)) 

After working on the above program, you need to see the next end result:

[11, 16, 21, 31]

We hope that after wrapping up this tutorial, you need to know a number of methods to test two lists for distinction. However, chances are you’ll apply more with examples to realize confidence. Also, to be taught Python from scratch to depth, do learn our step-by-step Python tutorial.

Learn more:

Pramod Kumar Yadav is from Janakpur Dham, Nepal. He was born on December 23, 1994, and has one elder brother and two elder sisters. He completed his education at various schools and colleges in Nepal and completed a degree in Computer Science Engineering from MITS in Andhra Pradesh, India. Pramod has worked as the owner of RC Educational Foundation Pvt Ltd, a teacher, and an Educational Consultant, and is currently working as an Engineer and Digital Marketer.



Leave a Comment