def find(needle, haystack):
"""
Descr:
Checks for the presence of a string/character(needle) in the
items of an iterable object
"""
if isinstance(haystack, list):
for h in haystack:
find(needle=needle, haystack=h)
elif isinstance(haystack, dict):
for k, v in haystack.items():
find(needle=needle, haystack=v)
else:
if str(needle) in haystack:
print(haystack)
# We can then call the function above as follows:
print("1. Search Results for e in '[eretail, retail, fax xerox]")
find(needle="e", haystack=[ "eretail", "retail", "fax" "xerox" ])
print("\n2. Search Results for x in '[eretail, retail, fax xerox]'")
find(needle="x", haystack=[ "eretail", "retail", "fax" "xerox" ])
print("\n3. Search Results for o in '[eretail, retail, fax xerox]'")
find(needle="o", haystack=[ "eretail", "retail", "fax" "xerox" ])
# Mixed Haystack illustration...
print("\n4. Search Results for cat in '[bobcat, [concatenate,boy, girl], dict(a=vacate, b=boy)]'")
find(needle="cat", haystack=["bobcat", ["concatenate", "boy", "girl"], dict(a="vacate", b="boy")])
# output:
1. Search Results for e in '[eretail, retail, fax xerox]
eretail
retail
faxxerox
2. Search Results for x in '[eretail, retail, fax xerox]'
faxxerox
3. Search Results for o in '[eretail, retail, fax xerox]'
faxxerox
4. Search Results for cat in '[bobcat, [concatenate,boy, girl], dict(a=vacate, b=boy)]'
bobcat
concatenate
vacate