Search code examples
pythonregexdictionarydefaultdict

How to create dictionary with list with regex and defaultdict


A dictionary is below

my = [{'Name':'Super', 'Gender':'Male', 'UNNO':111234},
      {'Name':'Spider', 'Gender':'Male', 'UNNO':11123},
      {'Name':'Bat', 'Gender':'Female', 'UNNO':113456},
     {'Name':'pand', 'Gender':'Female', 'UNNO':13456}]

The unique number is the value for key "UNNO" for each dictionary.

All UNNO numbers must contain 6 digits.

UNNO number start from 11 is only valid

Expected Out

my_dict_list = {'Male':['Super'], 'Female':['Bat']}

Original Code with out regex

d = {}
for i in my:
    if str(i['UNNO']).startswith('11') and len(str(i['UNNO'])) == 6:
        # To get {'Male':['Super'], 'Female':['Bat']}
        d[i['Gender']] = [i['Name']]

How to write with help of regex, wrote regular expression, how to complete with help of defaultdict

import re
from collections import defaultdict

# regular expression
rx = re.compile(r'^(?=\d{6}$)(?P<Male>11\d+)|(?P<Female>11\d+)')

# output dict
output = defaultdict(list)

Solution

  • To engage regex matching in solving your issue - use the following approach:

    import re
    from collections import defaultdict
    
    my_list = [{'Name': 'Super', 'Gender': 'Male', 'UNNO': 111234},
               {'Name': 'Spider', 'Gender': 'Male', 'UNNO': 11123},
               {'Name': 'Bat', 'Gender': 'Female', 'UNNO': 113456},
               {'Name': 'pand', 'Gender': 'Female', 'UNNO': 13456}]
    
    genders = defaultdict(list)
    pat = re.compile(r'^11\d{4}$')  # crucial pattern to validate `UNNO` number
    
    for d in my_list:
        if pat.search(str(d['UNNO'])):
            genders[d['Gender']].append(d['Name'])
    
    print(dict(genders))   # {'Male': ['Super'], 'Female': ['Bat']}