Flatten parts of list

I have this piece of code that creates a list of lists of size 3. The problem is that some of these lists of lists are size 1 and are nested in this manner:

[[['b']]]

And are found inside another list. Is there anyway of flattening only part of the lists of lists to remove these lists of size 1 from being nested(regardless of how nested they are) but maintaining them as lists? Here are 2 examples:

d = [[['a']], 'to', ['b']]

would become:

d = [['a'], 'to', ['b']]

and

d1 = [[['c']], 'from', [['a'], 'to', [['b']]]]

would become:

d1 = [['c'], 'from', [['a'], 'to', ['b']]]

I have tried to use isinstance and seeing if the list is nested and then replace the value at that index, but that has not been successful. Instead, my code has been replacing all the values and flattening the list, which is not my intention. I only wish to reduce nested lists of size 1 to a single list. This was my attempt:

for index, value in enumerate(d1):
    if isinstance(value, list) and len(value) == 1:
        d1[index] = d1[index][0]

In your second example you have lists inside of lists inside of lists (3 levels of nesting) that you want to flatten. But your code is only looking at 2 levels of nesting: d[index] and d[index][0] so your solution is only going to work on the outermost layers of nesting. Likewise your code will fail on an input like [[[[[1]]]]]. To fix it you'll need to generalize your solution so that it doesn't need to know or care how many levels deep the nested lists go.

Here's a solution using a recursive function - it's not the only way to solve the problem but it's the easiest to reason out, IMO.

def flatten(lst):
    # base case: if input is not a list, just return it
    if not isinstance(lst, list):
        return lst

    if len(lst) == 1:
        if isinstance(lst[0], list):
            # if input is a 1 element list and that element is itself
            # a list, then flatten it
            return flatten(lst[0])
        # ...otherwise return the input, it's already flattened
        return lst

    # when there are multiple entries in the input, flatten each
    return [flatten(x) for x in lst]