for hostname in hosts_list: hostname = hostname.strip() host_site = hostname.split('-', 1) if len(host_site) != 2: continue host, site = host_site try: type_num_pattern = re.compile(r"([A-Za-z_]+)([0-9]+)") expect: print "some error" type_num_match = type_num_pattern.match(host) type, num = type_num_match.groups() if site not in constellation: constellation[site] = {} if type not in constellation[site]: constellation[site][type] = [] constellation[site][type].append(num)