Python – Like Index Common characters

Sometimes we come across this type of problem in which we require to intersect each element of one list with the other. This type of problems usually occurs in developments in which we have the combined information, like names and surnames in different lists. Let’s discuss certain ways in which this task can be performed.

Method #1 : Using list comprehension + zip() + intersection()
List comprehension does the task of intersecting the similar index elements. The task of zip function is concatenating the resultant string into a single list and return list.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate 
# Like Index Common characters
# using list comprehension + zip() + intersection()
  
# initializing lists 
test_list1 = ["Geeksfor", "is", "be"]
test_list2 = ['Geeks', 'sb', 'ste']
  
# printing original lists
print ("The original list 1 is : " + str(test_list1))
print ("The original list 2 is : " + str(test_list2))
  
# using list comprehension + zip()
# Like Index Common characters + intersection()
res = ["".join(list(set(i).intersection(set(j)))) for i, j in zip(test_list1, test_list2)]
  
# printing result 
print ("The list after element intersection is : " + str(res))

chevron_right


Output :

The original list 1 is : ['Geeksfor', 'is', 'be']
The original list 2 is : ['Geeks', 'sb', 'ste']
The list after element intersection is : ['sGek', 's', 'e']

 

Method #2 : Using map() + lambda + zip() + intersection()
The task of mapping each index element with each other is performed by map function in this method and the functionality of intersection is performed by lambda and intersection() function. This method works only in Python 2.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python code to demonstrate 
# Like Index Common characters
# using map() + lambda + zip() + intersection()
  
# initializing lists 
test_list1 = ["Geeksfor", "is", "be"]
test_list2 = ['Geeks', 'sb', 'ste']
  
# printing original lists
print ("The original list 1 is : " + str(test_list1))
print ("The original list 2 is : " + str(test_list2))
  
# using map() + lambda + zip() + intersection()
# Like Index Common characters
res = list(map(lambda(i, j): "".join(list(set(i).intersection(set(j)))), zip(test_list1, test_list2)))
  
# printing result 
print ("The list after element intersection is : " + str(res))

chevron_right


Output :

The original list 1 is : ['Geeksfor', 'is', 'be']
The original list 2 is : ['Geeks', 'sb', 'ste']
The list after element intersection is : ['sGek', 's', 'e']



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.