1

I am search through a list of lists like this ...

srchList = [ ['Gloucestershire', 'Tewkesbury', '324225'], ['Gloucestershire', 'Tirley', '353925'], ['Gloucestershire', 'Westbury Court Garden', '354153'], ['Gloucestershire', 'Westonbirt', '354163'], ['Gloucestershire', 'Winchcombe', '354244'], ['Gloucestershire', 'Withington, '354270'], ['Gloucestershire', 'Woodchester Park (Nt)', '354288'], ['Gloucestershire', 'Wotton', '354320'], ['Greater London', 'Tennis Club Wimbledon', '324383'], ['Greater London', 'Arsenal F.C.', '350150'], ['Greater London', 'Barking', '324164'], ['Greater London', 'Barnet', '324151'], 
['Greater London', 'Beckton', '350286'], ['Greater London', 'Bexley', '350413'] ]

In order to select those in London, I have to enter "Greater London"

I have evaded problems with capitalisation with .upper() but how can I get a hit if I just enter a partial string, such as "london" or "Gloucester" ?

This is my current code ...

result = [region for region in srchList if region[0].upper() == search_item.upper()]
if result == []:
    print("Search item not found")
else:
    print(result)
wjandrea
  • 28,235
  • 9
  • 60
  • 81
Dave
  • 27
  • 4

1 Answers1

0

Try this instead (it checks to see if what you search for is anywhere in any of the regions in the list using the in operator)

result = [region for region in srchList if search_item.upper() in region[0].upper()]
if result == []:
    print("Search item not found")
else:
    print(result)
Daniel H.
  • 301
  • 2
  • 13