Python Forum
What is a faster way to deep copy a nested list without using .deepcopy()?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What is a faster way to deep copy a nested list without using .deepcopy()?
#1
I have two variables with the following structures:
    
# initialization
    O1 = [[] for g in range(n)] 
    for i in range(n):
        O1[i] = [[] for g in range(m)] 
        
    O2 = [[] for g in range(n)] 
    for i in range(n):
        O2[i] = [[] for g in range(m)] 
Then, I input these two into an initial function where different values are appended to them as:

for i in range(n):
    for j in range(m):
        O1[i][j].append([s, as, pat, 0])
        O2[i][j].append([[ol, oa, os, ot],[dl, da, ds],tpat, tp, 0])
As you can see, the internal structure of the lists gets complicated, especially in the case of O2. After this value assignment to O1 and O2, they are input into several other functions. Each function needs to make a deep copy of O1 and O2 and modify the copy for its own use without changing the original O1 and O2 variables. The changes include both .remove() and .append() and also +/- of values in the internal lists. The important thing is that no matter the changes, the original O1 and O2 should not change at all. This process runs iteratively where first O1 and O2 are assigned new values and then input into several other functions and copied and edited without any change in the original O1 and O2 that were used as input.
Using .deepcopy() is the only way that I know but as this is an iterative process, .deepcopy() function slows the code significantly down especially when O1 and O2 are large. I have tried using tuples as an immutable data structure but given the initial and internal structure and the changes made in the functions, it does not work. using tuples prevent the append() and remove() changes but not +/- operations.
I would be grateful if someone could suggest a faster way to do this.
Reply
#2
Forget about copies, that can only lead to confusion!

Use dictionaries, whose keys are the same, for example your O1

from string import ascii_lowercase, ascii_uppercase
 
# make 2 identical dictionaries with key: list    
d1 = {key:[] for key in ascii_lowercase}
d2 = {key:[] for key in ascii_lowercase}

# give d1[key] values
for key in d1.keys():
    d1[key] = ['A']

# assign d2[key] = d1[key]
for key in d2.keys():
    d2[key] = d1[key]

# now both d1 and d2 have the same (key: value) pairs, d2 taken from d1
d2['a'] # returns['A']
# now change d1[key]
d1['a'] = ['Z']
# d2['a'] remains ['A']
d2['a'] # returns ['A']
d1['a'].append('Y')
d1['a'] # returns ['Z', 'Y']
# d2['a'] remains ['A']
d2['a'] # returns ['A']
Also, the time taken to get a value from a dictionary is always the same I read.

And if your base data is very large, use generators.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Why is the copy method name in python list copy and not `__copy__`? YouHoGeon 2 1,359 Apr-04-2024, 01:18 AM
Last Post: YouHoGeon
  Copy List Not Copying BAbdulBaki 3 1,480 Aug-19-2023, 02:03 AM
Last Post: perfringo
  List all possibilities of a nested-list by flattened lists sparkt 1 1,791 Feb-23-2023, 02:21 PM
Last Post: sparkt
  About list copy. water 3 2,530 Apr-03-2022, 02:42 AM
Last Post: deanhystad
  Updating nested dict list keys tbaror 2 1,945 Feb-09-2022, 09:37 AM
Last Post: tbaror
  Python Program to Find the Total Sum of a Nested List vlearner 8 8,107 Jan-23-2022, 07:20 PM
Last Post: menator01
  deep learning python Stevedas 1 2,321 Sep-26-2021, 08:32 AM
Last Post: Yoriz
  Looping through nested elements and updating the original list Alex_James 3 2,920 Aug-19-2021, 12:05 PM
Last Post: Alex_James
Question Making a copy list in a function RuyCab 1 2,392 Jul-11-2021, 02:06 PM
Last Post: Yoriz
  shuffle a nested list giorgosmarga 11 14,615 Nov-12-2020, 07:04 PM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020