Canonical conversion of dict of dicts to list of dicts

Greg Ewing greg.ewing at canterbury.ac.nz
Thu Apr 1 02:32:32 EDT 2021


On 31/03/21 7:37 pm, dn wrote:
> Python offers mutable (can be changed) and immutable (can't) objects
> (remember: 'everything is an object'):
> https://docs.python.org/3/reference/datamodel.html?highlight=mutable%20data

While that's true, it's actually irrelevant to this situation.

>    $ a = "bob"
>    $ b = a
>    $ b = "bert"
>    $ a
>   'bob'

Here, you're not even attempting to modify the object that is
bound to b; instead, you're rebinding the name b to a different
object. Whether the object to which b was previously bound is
mutable or not makes no difference.

You can see this if you do the equivalent thing with lists:

 >>> a = ["alice", "bob", "carol"]
 >>> b = a
 >>> b
['alice', 'bob', 'carol']
 >>> b = ['dave', 'edward', 'felicity']
 >>> a
['alice', 'bob', 'carol']
 >>> b
['dave', 'edward', 'felicity']

-- 
Greg


More information about the Python-list mailing list