TransWikia.com

filter list of list by matching with lists in other list

Stack Overflow Asked on December 9, 2021

I have 2 lists of lists and I want to filter list1 based on matching with elements in list2:

list1 = [[1,2,3], [4,5,3]]
list2 = [[9,8,3], [7,10,6]]

I want to get the elements from list1 (list) that have the same last element in elements(which have the same position) in list2

In my example

list1[0][-1] is similar to list2[0][-1]

I want to compare only lists on the same position; means list1[0] vs list2[0] and list1[1] vs list2[1] and list1[2] vs list2[2] and so on (only lists with the same position)

One Answer

try this,

print([x for x, y in zip(list1, list2) if x[-1] == y[-1]])

[[1, 2, 3]]

Answered by sushanth on December 9, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP