boldbrandywine boldbrandywine - 6 months ago 12
Python Question

Creating a dictionary from a list of strings

I have a list of strings

list = ['2(a)', '2(b)', '3', '3(a)', '1d', '5']

where it is intentional that the 1d, 3, and 5 don't involve parentheses.

I would like to create a dictionary which looks like this:

dict = {'2': 'a', '2': 'b', '3': 'a', '1': 'd'}


dict = {'2': ['a', 'b'], '3': ['a'], '1': ['d']}.

Essentially, ignore those strings without a letter a-z. I've used regular expressions to extract from the top list the following:

['a', 'b', 'a', 'd'],

but this hasn't helped me much in forming my dictionary easily.

Any help is much appreciated.


This is a good time to use setdefault() for a dictionary. The first part involves capturing the numbers from the elements using a regex that captures all numbers. That list is then concatenated using join(). We then extract only alphabet characters in a list comprehension, joining them together once again. Lastly a check to see that both key and value exist so that we can set our dictionary to be of this format -> { '' : [] , ...}

import re

def to_dict(l):
    d = {}
    for i in l: 
        key = re.findall(r'\d+', i)
        value = ''.join(j for j in i if j.isalpha())
        if key and value:
            d.setdefault(''.join(key), []).append(value)    
    return d

Sample output:

l = ['2(a)', '2(b)', '3', '3(a)', '1c', '5']
print to_dict(l)
>>> {'1': ['c'], '3': ['a'], '2': ['a', 'b']}