Skip to content
Related Articles
Open in App
Not now

Related Articles

Python | Extract similar index elements

Improve Article
Save Article
  • Last Updated : 21 Jan, 2023
Improve Article
Save Article

Sometimes, while working with Python data, we can have a problem in which we require to extract the values across multiple lists which are having similar index values. This kind of problem can come in many domains. Let’s discuss certain ways in which this problem can be solved. 

Method #1 : Using loop + zip() The combination of above functions can be used to solve this problem. In this, we extract combine the index elements using zip and then extract and check for similarity using conditional statement in loop. 

Python3




# Python3 code to demonstrate working of
# Extracting similar index elements
# using loop + zip()
 
# initialize lists
test_list1 = ["a", "b", "c", "d"]
test_list2 = ["g", "b", "s", "d"]
 
# printing original lists
print("The original list 1 : " + str(test_list1))
print("The original list 2 : " + str(test_list2))
 
# Extracting similar index elements
# using loop + zip()
res = []
for i, j in zip(test_list1, test_list2):
    if i == j:
        res.append(i)
 
# printing result
print("Similar index elements in lists : " + str(res))

Output : 

The original list 1 : ['a', 'b', 'c', 'd']
The original list 2 : ['g', 'b', 's', 'd']
Similar index elements in lists : ['b', 'd']

  Method #2 : Using zip() + list comprehension Combination of these functionalities can also be used to solve this problem. In this, we use similar method as above, just a shorthand logic compressed using list comprehension. 

Python3




# Python3 code to demonstrate working of
# Extracting similar index elements
# using list comprehension + zip()
 
# initialize lists
test_list1 = ["a", "b", "c", "d"]
test_list2 = ["g", "b", "s", "d"]
 
# printing original lists
print("The original list 1 : " + str(test_list1))
print("The original list 2 : " + str(test_list2))
 
# Extracting similar index elements
# using list comprehension + zip()
res = [i for i, j in zip(test_list1, test_list2) if i == j]
 
# printing result
print("Similar index elements in lists : " + str(res))

Output : 

The original list 1 : ['a', 'b', 'c', 'd']
The original list 2 : ['g', 'b', 's', 'd']
Similar index elements in lists : ['b', 'd']

Method #3 : Using numpy.intersect1d()

note: install numpy module using command “pip install numpy”

This method makes use of the numpy library. The intersect1d function returns the sorted, unique values that are in both of the input arrays.

Python3




# Python3 code to demonstrate working of
# Extracting similar index elements
# using numpy.intersect1d()
 
import numpy as np
 
# initialize lists
test_list1 = ["a", "b", "c", "d"]
test_list2 = ["g", "b", "s", "d"]
 
# printing original lists
print("The original list 1 : " + str(test_list1))
print("The original list 2 : " + str(test_list2))
 
# Extracting similar index elements
# using numpy.intersect1d()
res = np.intersect1d(test_list1, test_list2)
 
# printing result
print("Similar index elements in lists : " + str(res))
 
#this code is contributed by edula vinay kumar reddy

Output :
The original list 1 : [‘a’, ‘b’, ‘c’, ‘d’]
The original list 2 : [‘g’, ‘b’, ‘s’, ‘d’]
Similar index elements in lists : [‘b’ ‘d’]

Method #3 has the same time and space complexity O(n) as it also iterates through the lists once.
 


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!