I have a list that can have up to three elements, and I want to append them to three list, because I get this list from another element within a loop. My final goal is to have a dataframe with the data of all elements. I have coded this:
affected_projects_1 = []
affected_projects_2 = []
affected_projects_3 = []
for issue in project:
affected_projects = issue.affected_projects
try:
affected_projects_1.append(affected_projects[0])
except IndexError:
affected_projects_1.append('')
except:
raise Exception
try:
affected_projects_2.append(affected_projects[1])
except IndexError:
affected_projects_2.append('')
except:
raise Exception
try:
affected_projects_3.append(affected_projects[2])
except IndexError:
affected_projects_3.append('')
except:
raise Exception
df = pd.DataFrame(zip(affected_projects_1, affected_projects_2, affected_projects_3))
This piece of code seems pretty verbose and almost repetitive (the only thing that changes is the number). How could I make it more compact? Is that even possible without losing readability or using antipatterns (i.e using globals variables). Thanks