In other words, if it's found that "f" is in the 4th position of the sublist, return that sublist, otherwise, exclude it if "f" is not found.
List = [['a','b','c','d','f'],['a','b','c','d','e'],['a','b','c','d','e'],['a','b','c','f','f'],['a','b']]
I have the following function which would work if all the sublists were the same size.
def Function(SM):
return filter(lambda x: re.search("f",str(x[4])),List)
IndexError: list index out of range
Desired_List = [['a','b','c','d','f'],['a','b','c','f','f']]
I'm reluctant to use a for loop, because of the speed and efficiency costs. Are there any alternatives that are just as quick?
You can use list comprehension:
lst = [['a','b','c'], ['a','b','c','d','f'],['a','b','c','d','e'],['a','b','c','d','e'],['a','b','c','f','f'],['a','b']]
lst_desired = [l for l in lst if len(l) >= 5 and l[4] == "f"]
print lst_desired
Output
[['a', 'b', 'c', 'd', 'f'], ['a', 'b', 'c', 'f', 'f']]