I have following function, which takes values from pandas dataframe columns and supplies arguments(s0_loc,s1_loc,.. upto ..,s12_loc if and only if their respective s0,s1,s2...,s12 is not-null) to another function. Also It will check whether s1 is null or not if and only if s0 is not null... Similarly it will check whether s2 is null or not iff s0,s1 is not null.. So on..
Based on above criteria I have written following function. But it's lengthy function... I want to reduce the piece of code in this function.
def compare_locality(p,p_loc,s0,s0_loc,s1,s1_loc,s2,s2_loc,s3,s3_loc,s4,s4_loc,s5,s5_loc,s6,s6_loc,s7,s7_loc,s8,s8_loc,s9,s9_loc,s10,s10_loc,s11,s11_loc,s12,s12_loc):
loc = []
if s0 != '' :
loc.append(s0_loc)
if s1 != '' :
loc.append(s1_loc)
if s2 != '' :
loc.append(s2_loc)
if s3 != '' :
loc.append(s3_loc)
if s4 != '' :
loc.append(s4_loc)
if s5 != '' :
loc.append(s5_loc)
if s6 != '' :
loc.append(s6_loc)
if s7 != '' :
loc.append(s7_loc)
if s8 != '' :
loc.append(s8_loc)
if s9 != '' :
loc.append(s9_loc)
if s10 != '' :
loc.append(s10_loc)
if s11 != '' :
loc.append(s11_loc)
if s12 != '' :
loc.append(s12_loc)
if len(loc) == 0:
return ''
else:
return compare(p_loc,*loc)
Can I get any suggestion on how to achieve this??