I have a question that is similar to this question, but mine concerns multidimentional lists like this:
myList = [['abc', 'abc-321'], ['def', '789-abc'], ['xyz', 'xyz-123']]
newList = []
I want to search this myList for a certain word/phrase, and if there are matches, the entire sublist to be appended to newList, with a way to control with elements are searched
For example, If I search for the the tern ‘abc’ in the first and second element of each sublist, newList should be (two matches):
[['abc', 'abc-321'], ['def', '789-abc']]
but if only the first element is to be searched, newList should be (only one match):
[['abc', 'abc-402']]
How can I do both types of searches in the most efficient way? Please consider this: The list to be searched contains around a thousand sublists and the text to be searched is about 1-2 paragraphs on average.
Try This
Note: This simply utilized List Comprehension. The only thing to note is the comparison. Comparing two string with
inwill try to match the first string any where in the second string. Comparing a string with a list, it will try to match the first string anywhere in the first element of the string.A simpler implementation