Python how to search for an item in a nested list

Let's say I have this list:

li = [["0", "20", "ar"], ["20", "40", "asdasd"], ["50", "199", "bar"], ["24", "69", "sarkozy"]] 

Now, forget about the numbers, this is what allowed me to recognize the position of the line. So basically, given that I have the string "ar" in my hand, how can I extract all lists containing "ar"?

 new_li = [["50", "199", "bar"], ["24", "69", "sarkozy"]] 

How can I get this list?

+4
source share
1 answer
 >>> [x for x in li if 'ar' in x[2]] [['0', '20', 'ar'], ['50', '199', 'bar'], ['24', '69', 'sarkozy']] 
+11
source

All Articles