Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

So, I`m getting some values from large nested dictionary. s_copy['productStyleVariantSizeInfo']['sizeDimension2SizeOptions'] and f_copy['productStyleVariantSizeInfo']['sizeDimension2SizeOptions'] could be a list of dicts or dicts if there is only one element. I want to know how to make less code here or just in that huge elif block. thanks

            if 'sizeDimension2SizeOptions' not in f_copy['productStyleVariantSizeInfo']:
                if isinstance(f_copy['productStyleVariantSizeInfo']['sizeDimension1SizeOptions'], list):
                    one_size_dict = {}
                    first_dimension_list = f_copy['productStyleVariantSizeInfo']['sizeDimension1SizeOptions']
                    for dict_size in first_dimension_list:
                        one_size_dict.update({dict_size['sizeOptionId']: dict_size['sizeOptionName']})
                    return one_size_dict

                elif isinstance(f_copy['productStyleVariantSizeInfo']['sizeDimension1SizeOptions'], dict):
                    coupled_size_dict = {}
                    #first_dimension_list is dictionary
                    first_dimension_list = f_copy['productStyleVariantSizeInfo']['sizeDimension1SizeOptions']
                    #one_dimension_list is list with one dictionary
                    one_dimension_list.append(first_dimension_list)
                    for dict_size in one_dimension_list:
                        coupled_size_dict.update({dict_size['sizeOptionId']: dict_size['sizeOptionName']})
                    return coupled_size_dict

            elif 'sizeDimension2SizeOptions' in s_copy['productStyleVariantSizeInfo']:
                if isinstance(f_copy['productStyleVariantSizeInfo']['sizeDimension1SizeOptions'], list):
                    first_dimension_list = f_copy['productStyleVariantSizeInfo']['sizeDimension1SizeOptions']
                    sizes1.append(first_dimension_list)
                    [two_dimension_size_list.append(size1_dict) for size1_dict in first_dimension_list]
                else:
                    first_dimension_list = [f_copy['productStyleVariantSizeInfo']['sizeDimension1SizeOptions']]
                    sizes1.append(first_dimension_list)
                    [two_dimension_size_list.append(size1_dict) for size1_dict in first_dimension_list]

                if isinstance(s_copy['productStyleVariantSizeInfo']['sizeDimension2SizeOptions'], list):
                    second_dimension_list = s_copy['productStyleVariantSizeInfo']['sizeDimension2SizeOptions']
                    sizes1.append(second_dimension_list)
                    [two_dimension_size_list.append(size2_dict) for size2_dict in second_dimension_list]
                    return dict((str(x['sizeOptionId']), str(x['sizeOptionName'])) for x in two_dimension_size_list)
                else:
                    second_dimension_list = [s_copy['productStyleVariantSizeInfo']['sizeDimension2SizeOptions']]
                    sizes1.append(second_dimension_list)
                    [two_dimension_size_list.append(size2_dict) for size2_dict in second_dimension_list]
                    return dict((str(x['sizeOptionId']), str(x['sizeOptionName'])) for x in two_dimension_size_list)
share|improve this question

closed as unclear what you're asking by jonrsharpe, Simon André Forsberg, BeetDemGuise, syb0rg, Jamal Jun 11 at 15:52

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question.If this question can be reworded to fit the rules in the help center, please edit the question.

    
To make life easier for reviewers, please add sufficient context to your question. The more you tell us about what your code does and what the purpose of doing that is, the easier it will be for reviewers to help you. See also this meta question –  Simon André Forsberg Jun 11 at 11:19
add comment

1 Answer

up vote 3 down vote accepted
  • Instead of repeating lengthy expressions, assign them to local variables. For example:

    f_info = f_copy['productStyleVariantSizeInfo']
    s_info = s_copy['productStyleVariantSizeInfo']
    
  • When you must process irregularly structured data, you should make it regular before you go any further. In your case, doing this first should enable much simpler code to follow:

    first_dimension_list = f_info['sizeDimension1SizeOptions']
    if not isinstance(first_dimension_list, list):
        first_dimension_list = [first_dimension_list]
    
share|improve this answer
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.