Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nested dictionaries or tuples for key?

Tags:

People also ask

Is tuples can be used a keys in dictionaries?

A tuple containing a list cannot be used as a key in a dictionary. Answer: True. A list is mutable. Therefore, a tuple containing a list cannot be used as a key in a dictionary.

Which is faster tuple or dictionary in C#?

The Tuple method is similar to the above snippets, and while it is faster than the Dictionary<int, KeyValuePair<string, string>> , it is still nowhere near as fast as indexing directly into the collection to the desired value based on a hashed key, as is done in the MultiKeyDictionary class.

Should I use tuple or dictionary?

A tuple can contain different values with different datatype while a dictionary can contain only one datatype value at a time. Tuples are particularly useful for returning multiple values from a function. A dictionary can be used as a model object.

Can dictionaries be used as keys of a Python dictionary?

A dictionary or a list cannot be a key. Values, on the other hand, can literally be anything and they can be used more than once.


Suppose there is a structure like this:

{'key1' : { 'key2' : { .... { 'keyn' : 'value' } ... } } } 

Using python, I'm trying to determine advantages/disadvantages of two different approaches:

{'key1' : { 'key2' : { .... { 'keyn' : 'value' } ... } } } # A. nested dictionary {('key1', 'key2', ...., 'keyn') : 'value'} # B. a dictionary with a tuple used like key 

Then I'm interested to know, what is the best (A or B) in terms of:

  • Memory occupation
  • Complexity in insertion (considering collision-avoiding alorithms, etc...)
  • Complexity in find