I have this function which parses Beautiful Soup elements:
def get_info(): holder = [] problem_dict = {} selection_container = [] for moselect in base_page.find_all('div', attrs={'class': 'Moselect'}): if moselect.find('div', attrs={'id': 'noSize'}): holder.append(moselect.find_all('div')[1:]) for held_item in holder[0]: if held_item.find_all('option')[1:]: for m in held_item.find_all('option')[1:]: append_dict(selection_container, m) if len(m.text.split(' ')) > 1: stock_info = { 'message': reduce(lambda x, y: x + ' ' + y, m.text.split(' ')[1:])[1:-1], 'status': 'pre-order' } problem_dict.update({'stock': stock_info}) else: for m in held_item.find_all('option'): append_dict(selection_container, m) if len(m.text.split(' ')) > 1: stock_info = { 'message': reduce(lambda x, y: x + ' ' + y, m.text.split(' ')[1:])[1:-1], 'status': 'pre-order' } problem_dict.update({'stock': stock_info}) problem_dict.update(dict(selections=selection_container)) return problem_dict It will return dict with:
problem_dict = {'selection': [{'key': 'value'},{'key': 'value'},]} and sometimes it returns:
problem_dict = {'selection': [{'key': 'value'},{'key': 'value'}], 'stock': {'key': 'value', 'key': 'value'} } How can I refactor and optimize this method?