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)