Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

References to mutables (e.g., lists) as values in Python dictionaries - what is best practice?

It is possible to map a dictionary key to a value that is a reference to a mutable object, such as a list. Such a list object can be changed by invoking a list method on the reference, and the changes will be reflected in the dictionary. This is discussed in:

  • Python: How do I pass a variable by reference? and
  • Python : When is a variable passed by reference and when by value?

My question

Is it a good idea to map a dictionary key to the reference of a mutable object as opposed to mapping the key to an unnamed value?

In other words, it is better to create a dictionary with:

In [74]: x = {'a': somelist}

or:

In [74]: x = {'a': somelist[:]}

And is it better to change a dictionary value though citing that value by reference, e.g.:

In [77]: somelist.remove('apples')

or by its dictionary index:

In [77]: x['a'].remove('apples')

Discussion and research

A reference provides a nice handle that can improve the readability of, say, a function. As far as I can tell, however, the fact that a dictionary value was originally bound to a reference is lost once the value is created; one cannot see this fact when displaying a dictionary.

On the other hand, I am not sure this matters, because the reference and the value are the same object, and if one deletes the reference, the value object itself remains.

As I see it:

In [73]: somelist = ['apples', 'oranges', 'lemons', 'tangerines']
In [74]: x = {'a': somelist}
In [75]: x
Out[75]: {'a': ['apples', 'oranges', 'lemons', 'tangerines']}

In dictionary x, key 'a' maps to value somelist, though I do not see a way to verify that the value is associated with the reference somelist.

In [76]: x['a'] is somelist
Out[76]: True

This confirms that the list I see as the value is the same as the object pointed to by somelist.

In [77]: x['a'].remove('apples')
In [78]: x
Out[78]: {'a': ['oranges', 'lemons', 'tangerines']}

Since the value of 'a' in dictionary x is a list, I can remove an item from the list using the list method remove on object x['a'].

In [79]: somelist.remove('lemons')
In [80]: x
Out[80]: {'a': ['oranges', 'tangerines']}

Alternatively, I can use the method remove on object somelist.

In [81]: del somelist

I can delete the reference to the list, but the list object itself remains as the value associated with key a.

In [82]: x['a'].remove('oranges')

In [83]: x
Out[83]: {'a': ['tangerines']}
like image 942
Tom Baker Avatar asked Jun 17 '15 04:06

Tom Baker


People also ask

When should you use a dictionary rather than a list in Python?

Therefore, the dictionary is faster than a list in Python. It is more efficient to use dictionaries for the lookup of elements as it is faster than a list and takes less time to traverse. Moreover, lists keep the order of the elements while dictionary does not.

What is dictionary in Python how is it different from list data type?

Both of these are tools used in the Python language, but there is a crucial difference between List and Dictionary in Python. A list refers to a collection of various index value pairs like that in the case of an array in C++. A dictionary refers to a hashed structure of various pairs of keys and values.

Can the value in a Python dictionary be a list?

It definitely can have a list and any object as value but the dictionary cannot have a list as key because the list is mutable data structure and keys cannot be mutable else of what use are they.

How do I use a list inside a dictionary in Python?

Let's see all the different ways we can create a dictionary of Lists. Method #2: Adding nested list as value using append() method. Create a new list and we can simply append that list to the value. Iterate the list and keep appending the elements till given range using setdefault() method.


1 Answers

As I understand your question, the answer is that it depends what you want to do. If you want to set the dictionary value to an independent copy of some other value, then make a copy. If you want to set it to the same object, then don't make a copy.

In some cases you may want the dictionary to refer to the same object as other references, because this allows the dictionary to "see" changes to that object. In some cases you may want the dictionary to have its own copy, because you don't want other code that changes the object to affect your dictionary. It just depends on what the mutable object is and how your code as a whole is using it.

like image 167
BrenBarn Avatar answered Oct 15 '22 21:10

BrenBarn