Skip to content

List manip

ensure_list(ambiguous_item)

If input is not a list, return list of input

Source code in automation/utils/list_manip.py
4
5
6
def ensure_list(ambiguous_item):
    """If input is not a list, return list of input"""
    return ambiguous_item if isinstance(ambiguous_item, list) else [ambiguous_item]

flatten_list(my_list)

For a list containing embedded lists, generate 1d list with no 'None' vals

Source code in automation/utils/list_manip.py
 9
10
11
12
13
14
15
16
17
18
def flatten_list(my_list) -> Generator:
    """For a list containing embedded lists, generate 1d list with no 'None' vals"""
    for item in my_list:
        if not item:
            continue
        if isinstance(item, Iterable) and not isinstance(item, str):
            for x in flatten_list(item):
                yield x
        else:
            yield item

list_to_or(entry)

Given string or list, return items as string joined OR

Source code in automation/utils/list_manip.py
21
22
23
24
25
def list_to_or(entry):
    """Given string or list, return items as string joined OR"""
    entry = [entry] if not isinstance(entry, list) else entry
    entry = [str(i) for i in entry]
    return " or ".join(entry)