Python - Filter unequal elements of two lists corresponding same index


The indexing is the specific position of the element from the list. The unequal elements of two lists refer to those indexes whose elements are not matched from both lists. In Python, we have some built-in functions like enumerate(), zip(), filter(), etc. that can be used to filter unequal elements of two lists corresponding same index.

Let’s take an example of this

Given list,

lst_1 = [10, 20, 30, 40, 50]

lst_2 = [1, 20, 3, 40, 50]

Then the final result becomes [20, 40, 50] these elements from the list matches the corresponding same index.

Syntax

The following syntax is used in the examples-

enumerate()

The enumerate() is a built-in function in Python that allows keeping track of iterates over loops.

zip()

The zip() is a built-in function in Python that accepts any kind of parameters- lists, tuples, dictionaries, sets, and, so on.

filter()

The filter() method is applied when we need to filter the items based on specific conditions. In simple terms, it allows users to iterate those elements that are extracted to satisfy the requirement.

lambda

This lambda function in Python is known as an anonymous function. It can be used when function objects are required.

list()

The list() is built-in Python that creates the list object, accepts an iterable construct, and converts it into the list.

range()

The range() is a built-in function in Python that returns the sequence of elements between a given range. By default, the initial range always starts with 0 and ends with by assigning a specific range.

append()

The append method in Python is used to add the element at the end of the list.

Using enumerate() Function

The program uses the built-in function enumerate() to keep track of the iteration of two lists by accepting a parameter named zip. Both these functions are used to filter the unequal elements from two lists and find the corresponding same index to print the result.

Example

In the following example, start creating the two input lists to set the random integer in the variables- list1 and list2. Then it will use the list comprehension where for loop and if-statement executed in one-liner along with enumerate() and zip() function. All these processes are stored in the variable res. Finally, we are printing the result with the help of str(res).

# initialize lists
list1 = ["abc", "bcd", "c", "d"]
list2 = ["g", "b", "s", "d"]
# using list comprehension and enumerate()
res = [i for i, (val1, val2) in enumerate(zip(list1, list2)) if val1 != val2]
# result print
print("Unequal index elements in lists : " + str(res))

Output

 Unequal index elements in lists : [0, 1, 2]

Using filter() and lambda Function

The program uses the built-in function filter() to remove the unequal element and the use of built-in function lambda will calculate the corresponding element from the two lists.

Example

In the following example, we will first create the two lists in the variables- l1 and l2. Then use the built-in function list() which accepts the iterable construct filter(). The filter() accepts two parameters- lambda (to check both the list for correspondence same index) and zip(combine two lists to iterate) and the new filter element store it in the variable calc. Next, create the list comprehension to store the new element list in the variable result. Then print the result with the help of a variable result.

L1 = [11, 2, 3, 4, 5]
L2 = [1, 2, 3, 6, 5]
calc = list(filter(lambda x: x[0] == x[1], zip(L1, L2)))
result = [x[0] for x in calc]
print("The corresponding same index from two lists:",result)

Output

 The corresponding same index from two lists: [2, 3, 5]

Using for Loop

The program use for loop to iterate through the first list and using if-statement it will check the condition for equivalent of two lists and return the result using the built-in function append().

Example

In the following example, we will start the program by creating two lists and store in the respective variables i.e. l1 and l2. Then use the empty list [] in the variable result that will store the list containing corresponding matches from two dictionaries. Now it will use the for loop where variable i iterate through the first list i.e. l1 by using built-in functions- range() and len(). Using if-statement it will check the condition for pattern match between l1 and l2. If the item matches found equal then it returns the filter list result through append() function. Finally, we are printing the output with the help of variable named result.

# create the item list
l1 = [1, 20, 3, 40, 5]
l2 = [1, 2, 30, 6, 5]
result = []
for i in range(len(l1)):
    if l1[i] == l2[i]:
        result.append(l1[i])
print("The corresponding same index from two lists:", result)

Output

 The corresponding same index from two lists: [1, 5]

Using Zip and list Comprehension

The program the built-in function zip() that allows to iterate more the two lists and using equal to ‘==’ operator in the if-statement it will check the correspondence same index.

Example

In the following example, start the program by initializing two variables- list_1 and list_2 to store the input list. Then use the list comprehension to create a new list result that contains the elements that are equal at the same index in both lists. The zip function is used to iterate over both lists continuously. The if-statement inside the list comprehension checks if the elements at the same index in both lists are equal or not. If they are equal, the element is added to the variable result. Finally, print the output with the help of a variable named result.

# input list
list_1 = [1, 2, 3, 4, 5]
list_2 = [1, 2, 9, 6, 5]
# using zip inside the list comprehension
result = [x for x, y in zip(list_1, list_2) if x == y]
print("The corresponding same index from two lists:", result)

Output

 The corresponding same index from two lists: [1, 2, 5]

Using simple list Comprehension

The program uses list comprehension where loops and condition expressions are defined in one-liner to filter the unequal element from two lists.

Example

In the following example, create the two input lists in the variables- lst_1 and lst_2 that will be used to filter the unequal element from two lists. Then use the list comprehension technique where variable i iterates the first list by using built-in function len() and range(). Next, if-statement check the condition for matches between two list to calculate the correspondence same index and return the filter result in the variable res. Finally, it prints the result with the help of a variable named res.

# input list
lst_1 = [100, 200, 300, 400, 500]
lst_2 = [100, 2, 300, 6, 50]
# list comprehension
res = [lst_1[i] for i in range(len(lst_1)) if lst_1[i] == lst_2[i]]
print("The corresponding same index from two lists:", res)

Output

The corresponding same index from two lists: [100, 300]

Conclusion

We discussed the various methods to solve the above problem statement. The filter unequal elements remove those elements that are not found to be similar in both lists. These programs are generally used in various applications such as Data analysis, Data Synchronization, Filter Search Results, and collaborative filtering.

Updated on: 16-Aug-2023

172 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements