-2

I have a dictionary like that:

dic = {'a':[['1'],['4']],'b':['1'],'c':['2']}

and I would like to remove the un-necessary lists to get:

newdict={'a':['1','4'],'b':'1','c':'2'}

How can I do that? Thanks!

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
LoC
  • 115
  • 7
  • Is it guaranteed that the maximum nesting depth of the list is 2? – Jaideep Shekhar Dec 02 '19 at 16:08
  • No, it can go way higher than 2 – LoC Dec 02 '19 at 16:08
  • Does this answer your question? [Flatten an irregular list of lists](https://stackoverflow.com/questions/2158395/flatten-an-irregular-list-of-lists). Applying it to your scenario is just calling one of the `flatten` recipes (wrapped in `list` call when `flatten` is a generator function) on each `dict` value. – ShadowRanger Dec 02 '19 at 19:51
  • 1
    Side-note: Please don't write new code in Python 2 if at all possible. It's going out of support for good in less than a month (01 Jan 2020 is end-of-life), so if you want your skills/code to work and be able to use new versions of Python without potentially huge unpatched security/stability bugs, you should really be targeting Python 3. – ShadowRanger Dec 02 '19 at 19:55
  • 1
    @LoC What would be the correct output for the input: `{'a':[[[['1'],['4']]],'3'],'b':['1'],'c':['2']}`? – ruohola Dec 03 '19 at 20:51
  • Please do reply @LoC. Does my post answer your question? I realise that in your output, single element lists are unlisted, although you say my answer works. Which is correct? – Jaideep Shekhar Dec 04 '19 at 15:52
  • Looks like there won't be a reply. @ruohola should I edit the post to answer the above-mentioned output or leave it as is? – Jaideep Shekhar Dec 06 '19 at 13:59
  • @JaideepShekhar Just leave it for now, since at least it seems like OP was happy with that, BUT it obviously doesn't actually answer the question OP has asked, since it will not give the correct output for his example input. – ruohola Dec 06 '19 at 14:03
  • Sorry for the delay, got a crazy time. I worked around and it's enough for me. Regarding the version of Python, for now I'm stocked with Python 2.7 because I use tools that are not yet working with Python 3. Will come later I guess :) – LoC Dec 09 '19 at 08:22

1 Answers1

-1

Well, if you are not concerned about speed or efficiency, I suppose this could work:

def flatten(l):
    '''
    Function to flatten a list of any level into a single-level list

    PARAMETERS:
    -----------
        l: preferably a list to flatten, but will simply create a single element list for others

    RETURNS:
    --------
        output: list
    '''
    output = []
    for element in l:
        if type(element) == list:
            output.extend(flatten(element))
        else:
            output.append(element)
    return output

dic = {'a':[[[['1'],['4']]],'3'],'b':['1'],'c':['2']}
newdict = {key: flatten(value) for key, value in dic.items()}
print(newdict)

gives, as expected:

{'a': ['1', '4', '3'], 'b': ['1'], 'c': ['2']}
Jaideep Shekhar
  • 808
  • 2
  • 7
  • 21
  • Working fine, thank you ! Did not take time to run on what I work for now, I'll try to test it on bigger dictionaries to see. – LoC Dec 02 '19 at 16:33
  • @LoC For your example input `dic = {'a':[['1'],['4']],'b':['1'],'c':['2']}` this gives the incorrect output of `{'a': ['1', '4'], 'b': ['1'], 'c': ['2']}`. The correct output would be `{'a': ['1', '4'], 'b': '1', 'c': '2'}`. – ruohola Dec 03 '19 at 20:35