Reputation: 1763
In Python you can do something like a list comprehension with a filter function to work through a list of dictionaries and find the ones you want:
warranty_info = [warranty for warranty in warranty_info if
args.warranty_keyword.lower() in warranty['ServiceLevelDescription'].lower()]
where warranty_info is a list of dictionaries and ServiceLevelDescription
is a field on which I want to filter. The result is a new list of dictionaries with only those that I want. In this case I am looking for all the dictionaries who have a user provided word (warranty_keyword
) in their ServiceLevelDescription
field.
I know how to accomplish this in PowerShell using a foreach loop - is there a "cleaner" way to do it like Python has?
Upvotes: 1
Views: 476
Reputation: 1763
I figured it out - it's actually even more elegant than Python (Grant's opinion):
$WarrantyInfo | Where-Object 'ServiceLevelDescription' -Match ^*pro*
The syntax has a lot that's implied. This will search the list of HashTables and for each one extract the key ServiceLevelDescription
- afterwards it will check if there is a match to whatever you put after the -Match
switch.
Upvotes: 2