Page 1 of 1

Copy of a dictionnary

Posted: Tue Aug 22, 2017 5:11 pm
by Apollo22
Hello,

I have a dictionnary that I want to copy into another to later check if any change was done in the first one. However dict.deepcopy() is not recognised and dict1 = dict(dict2) has the same effect as dict1 = dict2. Does anyone know a way around this ?

Re: Copy of a dictionnary

Posted: Tue Aug 22, 2017 6:31 pm
by dhylands
You can use copy() to create a shallow copy (tested on the unix version of micropython). There is a deepcopy implementation found in the copy module in micropython-lib:
https://github.com/micropython/micropyt ... py/copy.py

Re: Copy of a dictionnary

Posted: Wed Aug 23, 2017 8:17 am
by Apollo22
I'm sorry I wasn't precise enough, I have a dictionnary inside a dictionnary, like that :

Code: Select all

a['1'] = {
    'a' : 1,
    'b' : 2,
}
And in this case, if I do b = a.copy, and then change a, it changes b. But as I only have one entry in the first dictionnary, I can just do

Code: Select all

b = a['1].copy()

Re: Copy of a dictionnary

Posted: Wed Aug 23, 2017 5:55 pm
by deshipu
So you want deepcopy.

Re: Copy of a dictionnary

Posted: Thu Aug 24, 2017 4:43 am
by pythoncoder
Indeed. To me @dhylands resolved this query, unless I'm failing to grasp the problem.

Re: Copy of a dictionnary

Posted: Wed Sep 19, 2018 6:04 pm
by marcwagner
in python I would do:
[code] from copy import deepcopy
dict2 = deepcopy(dict1) [/code]
see: https://stackoverflow.com/questions/2465921/how-to-copy-a-dictionary-and-only-edit-the-copy

when I try this in micropython I get
[code]ImportError: no module named 'copy'[/code]

so what is the micropython way of doing this?

Re: Copy of a dictionnary

Posted: Wed Sep 19, 2018 7:25 pm
by jickster
marcwagner wrote:
Wed Sep 19, 2018 6:04 pm
in python I would do:

Code: Select all

from copy import deepcopy
dict2 = deepcopy(dict1)
see: https://stackoverflow.com/questions/246 ... t-the-copy

when I try this in micropython I get

Code: Select all

ImportError: no module named 'copy'
so what is the micropython way of doing this?

You have to add this .py file to micropython

https://github.com/micropython/micropyt ... py/copy.py

because deepcopy is not builtin.

Re: Copy of a dictionnary

Posted: Wed Jul 03, 2019 5:47 am
by manseekingknowledge
Based on your dict in a dict example it appears you are using only immutable objects (strings and integers), dicts, and lists in the thing you want to copy. If that is the case it would be much more efficient to do a json.dumps() followed immediately by a json.loads(). The deepcopy function has a lot of extra code that makes it inefficient for simple collections like those that can be represented by a JSON string. An additional bonus is that the json module is included in micropython by default.

Code: Select all

import json

a_dict = {
    '1': {
        'a': 1,
        'b': 2
    },
    '2': {
        'a': 3,
        'b': 4
    }
}

copy_of_a_dict = json.loads(json.dumps(a_dict))