Skip to content
Advertisement

Find matching values in a list of lists

I’m trying to iterate over a list of lists in python 2.7.5 and return those where the first value is found in a second list, something like this:

JavaScript

So I would want list3 to contain [['aa',1,3,7],['bc', 3, 4, 4]] but instead I just get the whole of list2.

Advertisement

Answer

Try a more simple approach that is closer to what you want:

JavaScript

You need e[0] since list2 is a list of lists. You can also write this in a single line using the filter() function:

JavaScript

or using list comprehension:

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement