Copy of a dictionnary

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
Apollo22
Posts: 6
Joined: Fri Jul 21, 2017 2:27 pm

Copy of a dictionnary

Post by Apollo22 » Tue Aug 22, 2017 5:11 pm

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 ?

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Copy of a dictionnary

Post by dhylands » Tue Aug 22, 2017 6:31 pm

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

Apollo22
Posts: 6
Joined: Fri Jul 21, 2017 2:27 pm

Re: Copy of a dictionnary

Post by Apollo22 » Wed Aug 23, 2017 8:17 am

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()

User avatar
deshipu
Posts: 1388
Joined: Thu May 28, 2015 5:54 pm

Re: Copy of a dictionnary

Post by deshipu » Wed Aug 23, 2017 5:55 pm

So you want deepcopy.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Copy of a dictionnary

Post by pythoncoder » Thu Aug 24, 2017 4:43 am

Indeed. To me @dhylands resolved this query, unless I'm failing to grasp the problem.
Peter Hinch
Index to my micropython libraries.

marcwagner
Posts: 2
Joined: Mon Aug 27, 2018 1:44 pm

Re: Copy of a dictionnary

Post by marcwagner » Wed Sep 19, 2018 6:04 pm

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?
Last edited by marcwagner on Thu Sep 20, 2018 7:46 pm, edited 1 time in total.

jickster
Posts: 629
Joined: Thu Sep 07, 2017 8:57 pm

Re: Copy of a dictionnary

Post by jickster » Wed Sep 19, 2018 7:25 pm

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.

manseekingknowledge
Posts: 61
Joined: Sun Oct 29, 2017 5:14 pm

Re: Copy of a dictionnary

Post by manseekingknowledge » Wed Jul 03, 2019 5:47 am

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

Post Reply