Feb-04-2021, 07:10 PM
Oh are we golfing? Generators make the world go round :)
>>> dictOfNames = [ ... {'sec': 'coalmine', 'country': 'usa', 'state': 'newyork', 'zip': '17304', 'amt': '15480', 'tg': '910-456-8415'}, ... {'sec': 'coalPower', 'country': 'uk', 'state': 'birmingham', 'zip': '51730', 'amt': '26663', 'tg': '321-854-7536'}, ... {'sec': 'hydelPower', 'country': 'usa', 'state': 'sanfrancisco', 'zip': '92416', 'amt': '748521', 'tg': '451-278-9632'} ... ] >>> def filter_by(data, key, value): ... for row in data: ... if key in row and value in row[key].lower(): ... yield row ... >>> list(filter_by(dictOfNames, 'sec', 'power')) [{'sec': 'coalPower', 'country': 'uk', 'state': 'birmingham', 'zip': '51730', 'amt': '26663', 'tg': '321-854-7536'}, {'sec': 'hydelPower', 'country': 'usa', 'state': 'sanfrancisco', 'zip': '92416', 'amt': '748521', 'tg': '451-278-9632'}]