Copy of a dictionnary
Copy of a dictionnary
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 ?
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
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
https://github.com/micropython/micropyt ... py/copy.py
Re: Copy of a dictionnary
I'm sorry I wasn't precise enough, I have a dictionnary inside a dictionnary, like that :
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
a['1'] = {
'a' : 1,
'b' : 2,
}
Code: Select all
b = a['1].copy()
Re: Copy of a dictionnary
So you want deepcopy.
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: Copy of a dictionnary
Indeed. To me @dhylands resolved this query, unless I'm failing to grasp the problem.
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
-
- Posts: 2
- Joined: Mon Aug 27, 2018 1:44 pm
Re: Copy of a dictionnary
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?
[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.
Re: Copy of a dictionnary
marcwagner wrote: ↑Wed Sep 19, 2018 6:04 pmin python I would do:see: https://stackoverflow.com/questions/246 ... t-the-copyCode: Select all
from copy import deepcopy dict2 = deepcopy(dict1)
when I try this in micropython I getso what is the micropython way of doing this?Code: Select all
ImportError: no module named 'copy'
You have to add this .py file to micropython
https://github.com/micropython/micropyt ... py/copy.py
because deepcopy is not builtin.
-
- Posts: 61
- Joined: Sun Oct 29, 2017 5:14 pm
Re: Copy of a dictionnary
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))