0

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??

Niper
  • 63
  • 1
  • 8
  • See https://stackoverflow.com/questions/17246693/what-is-the-difference-between-shallow-copy-deepcopy-and-normal-assignment-oper and/or https://stackoverflow.com/questions/2612802/how-to-clone-or-copy-a-list . Everything here works as it is supposed to: you create a new list, but the items of your list themselves are actually the same objects, so when you update them, you see them updated in your original list and the copy. You need a deepcopy to recursively create new, independant objects.. – Thierry Lathuille Nov 02 '20 at 13:19

1 Answers1

0

You can try using copy.deepcopy:

import copy

ind_template_copy2 = copy.deepcopy(ind_template)
ind_template[0][0] = 'test2'
print(ind_template)
print(ind_template_copy2)

Output for ind_template = [['a','b'],['c','d']]:

[['test2', 'b'], ['c', 'd']]
[['a', 'b'], ['c', 'd']]
Sushil
  • 5,440
  • 1
  • 8
  • 26
  • 1
    thanks...that worked perfectly. So do you think the raw .copy(), list and slice[:] have a bug?? as I'm sure they should have worked... Anyway...thats great thanks again – Niper Nov 02 '20 at 13:14
  • YW! If my ans has helped u, pls accept my ans as the best ans. Thanks! – Sushil Nov 03 '20 at 14:53