So, I fairly new to Python, around 6 months; but I'm aware that lists occupy the same memory, unless you copy the new list from the old list...
But I'm seeing strange behaviour when using lists...trying to load in a dataframe from a csv, then convert to a list; the end game is to ensure I have a template to use as a base.
load template from template.xlsx
create new row of list data default new row to template update new row add new row to master then repeat this section before writing out results to a new CSV.
excel_IND_TEMPLATE_df = pd.read_excel('template.xlsx',
sheet_name='IND_TEMPLATE', dtype=str)
excel_IND_TEMPLATE_df.fillna('', inplace=True)
ind_template = excel_IND_TEMPLATE_df.loc[0:0, :].values.tolist()
## using list
ind_template_copy1 = list(ind_template)
ind_template[0][0] = 'test1'
print(ind_template)
print(ind_template_copy1)
## using .copy()
ind_template_copy2 = ind_template.copy()
ind_template[0][0] = 'test2'
print(ind_template)
print(ind_template_copy2)
## using slice
ind_template_copy3 = ind_template[:]
ind_template[0][0] = 'test3'
print(ind_template)
print(ind_template_copy3)
This outputs the following: -
[['test1', 'Y']]
[['test1', 'Y']]
[['test2', 'Y']]
[['test2', 'Y']]
[['test3', 'Y']]
[['test3', 'Y']]
So essentially using the list, .copy and slice all force the new list to be the same memory as the original list...? I thought this would work...and end in 2 separate lists??