UniCoreFW

pluck()

Extract a list of property values from an array of objects.

Implementation

Args: array: An array of objects key: The property key to extract Returns: A list of the property values

Example

pluck([{'a': 1, 'b': 2}, {'a': 3, 'b': 4}], 'a')

Expected output: [1, 3]

Source Code

def pluck(array: List[dict], key: str) -> List[Any]: return [ obj.get(key) if isinstance(obj, dict) else getattr(obj, key, None) for obj in array ]