DeduplicateValuesbyKey

Given a list of objects and a key found in each of those objects, return a unique list of values associated with that key. Returns error if the objects provided do not contain the key of interest.

python · Common Scripts

Details

IDDeduplicateValuesbyKey
Languagepython
From Version6.0.0
Docker Imagedemisto/python3:3.12.13.10404775

README

Given a list of objects and a key found in each of those objects, return a unique list of values associated with that key. Returns error if the objects provided do not contain the key of interest.

Script Data


Name Description
Script Type python3
Tags  

Inputs


Argument Name Description
object_list List of objects (dictionaries).
key_of_interest String representing key from which unique values should be retrieved. Use dot notation to access subkeys (e.g. ‘key.subkey’)
keep_none Default is False. If set to True, will return None in the unique list if the key is not found

Outputs


Path Description Type
DeduplicatedValues List of unique values for the specified key Unknown
import pytest


def test_generate_unique_values_from_objects():
    from DeduplicateValuesbyKey import generate_unique_values_from_objects

    objects = [
        {"key": "value1", "value": "value1"},
        {"key": "value1", "value": "value2"},
        {"key": "value2", "value": "value3"},
        {"key": "value2", "value": "value4"},
        {"key": "value3", "value": "value5"},
        {"key": "value3", "value": "value6"},
    ]
    values = generate_unique_values_from_objects(objects, "key", False)
    assert set(values) == {"value1", "value2", "value3"}


def test_generate_unique_values_from_objects_with_none():
    from DeduplicateValuesbyKey import generate_unique_values_from_objects

    objects = [
        {"key": "value1", "value": "value1"},
        {"key": "value1", "value": "value2"},
        {"key": "value2", "value": "value3"},
        {"key": "value2", "value": "value4"},
        {"key": "value3", "value": "value5"},
        {"key": "None_value", "value": None},
    ]
    values = generate_unique_values_from_objects(objects, "key", True)
    assert set(values) == {"None_value", "value1", "value2", "value3"}


def test_generate_unique_values_from_objects_fail():
    from DeduplicateValuesbyKey import generate_unique_values_from_objects

    with pytest.raises(SystemExit):
        generate_unique_values_from_objects([], "key", True)