import itertools


def ensure_unique_key(new_key, dictionary):
    """If new_key is already in dictionary, a number is appended to new_key.

    If new_key is 'main' and 'main' is already in dictionary, the following keys
    are tried until one is not part of dictionary:

        main/0
        main/1
        main/2
        ...
        main/10
        main/11
        ...


    Args:
        new_key: The key to be added or modified.
        dictionary: The dictionary to check new_key against.
    Returns:
        new_key if the key is unique, otherwise an alternative version.

    >>> keys = ['a', 'a/0', 'a/1', 'a/2', 'a/3']
    >>> ensure_unique_key('a', keys)
    'a/4'
    >>> ensure_unique_key('b', keys)
    'b'
    >>> ensure_unique_key('a/1', keys)
    'a/1/0'
    """
    if new_key not in dictionary:
        return new_key

    for i in itertools.count():
        key = f'{new_key}/{i}'
        if key not in dictionary:
            return key