This depends a bit on exactly what you mean by partially.
First definition: the search
term should match exactly, but it can match at any point in the string. This is probably almost what you mean. In this case you really want to check if the sublist contains the search term. For this, you want to use Python's in
operator:
if search in sublist[1]:
print sublist
Because of the difference between equality and contains, this will be between slightly and very much slower. I doubt it matters to you.
Second definition: The same as the first, but case doesn't matter. In this case you want to normalize the case, basically just ignore upper or lower case by making them all the same, using Pythons lower
(or upper
) string methods.
search = 'Tom Clancy'
search_lower = search.lower() # move the search lowering
for sublist in list_of_books:
# since strings are immutable, sublist[1].lower() creates a new lower-cased
# string to be compared against search_lower. sublist[1] doesn't get modified
if search_lower in sublist[1].lower():
print sublist
That's probably what you want.
There's a third definition, which is "fuzzy matching". If you accept fuzzy matches, clincy
might match Clancy
. Heck, if the search is fuzzy enough tom
can match Clancy
. That's a whole 'nuther can of worms. Luckily, this Stack Overflow question has a whole bunch of libraries that can help with it.