I want to be able to store and look up values in a dictionary based on two integer values.
So when I look up a value I want to use the keys read_length
and min_size
to access the element, like so:
number_of_read_lengths[read_length][min_size]
I know I can create nested dictionaries, but that is a slight hassle.
Is there a simple way of doing what I want to do?
In python, if we want a dictionary in which one key has multiple values, then we need to associate an object with each key as value. This value object should be capable of having various values inside it. We can either use a tuple or a list as a value in the dictionary to associate multiple values with a key.
Dictionaries are sometimes found in other languages as “associative memories” or “associative arrays”. Unlike sequences, which are indexed by a range of numbers, dictionaries are indexed by keys, which can be any immutable type; strings and numbers can always be keys.
You can create a nested dictionary in Python by placing comma-separated dictionaries within curly braces {}. A Python nested dictionary allows you to store and access data using the key-value mapping structure within an existing dictionary.
One can only put one type of object into a dictionary. If one wants to put a variety of types of data into the same dictionary, e.g. for configuration information or other common data stores, the superclass of all possible held data types must be used to define the dictionary.
You can use any immutable and hashable object as key, including tuples
number_of_read_lengths = {} number_of_read_lengths[14,3] = "Your value"
Using tuples could be quite annoying -- you got to remember to place the tuple during indexing.
I would recommend a nested dict, but a defaultdict
, like so:
from collections import defaultdict number_of_read_lengths = defaultdict(dict) number_of_read_lengths[1][2] = 3 print(number_of_read_lengths)
This code would give:
defaultdict(<type 'dict'>, {1: {2: 3}})
This way, any non-existing element in the number_of_read_lengths
dict will be created as a dict when accessing or setting it. Simple and effective.
More info on defaultdict
: http://docs.python.org/library/collections.html#collections.defaultdict There are also examples: http://docs.python.org/library/collections.html#defaultdict-examples
You could try to use tuples as keys:
number_of_read_lengths[(read_length, min_size)]
Just to expand a bit more from the comment I made:
A dict
key must be hashable, which a simple tuple is. However, a tuple that contains unhashable values such as lists, is not hashable (even though it is immutable!) and therefore cannot be used as dict
key:
>>> bad = ([12],[32])
# still immutable
>>> bad[1] = [21]
Traceback (most recent call last):
File "<input>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
# but not hashable!
>>> d = {}
>>> d[bad] = 2
Traceback (most recent call last):
File "<input>", line 1, in <module>
TypeError: unhashable type: 'list'
You can even have mutable and hashable objects as dict
keys, but it's not really useful and should be avoided.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With