Python `dict` indexed by tuple: getting a slice of pie

Let's say I have

my_dict = {
  ("airport", "London"): "Heathrow",
  ("airport", "Tokyo"): "Narita",
  ("hipsters", "London"): "Soho"
}

      

What is an efficient (without scanning all keys) but elegant way to deduce all airports from this dictionary, i.e. the expected output ["Heathrow", "Narita"]

. In databases that can be indexed by tuples, you can usually do something like

airports = my_dict.get(("airport",*))

      

(but usually only with stars sitting at the rightmost points in the tuple, since the index is usually stored in only one order).

Since I am assuming Python is indexing a dictionary using tuple keys in a similar way (using inline key ordering), I suppose there might be a method I could use to truncate the index in this way?

Edit1: Added expected result

Edit2: last phrase removed. Added "(without scanning all keys)" to make it clearer.

+3


source to share


3 answers


The way your data is currently organized prevents an efficient search - essentially, you need to scan all the keys.

Dictionaries are behind the scenes hash tables and the only way to access the value is to get the hash of the key - and for that you need the whole key.



Use a nested hierarchy like this, so you can do a direct O (1) search:

my_dict = {
  "airport": {
     "London": "Heathrow",
     "Tokyo": "Narita",
  },
  "hipsters": {
     "London": "Soho"
  }
}

      

+4


source


Check is "airport"

present in every dictionary key.

Demo

>>> [value for  key, value in my_dict.items() if  "airport" in key]
['Narita', 'Heathrow']
>>> 

      




Yes, the Nested Dictionary would be the best option.

>>> my_dict = {
...   "airport": {
...      "London": "Heathrow",
...      "Tokyo": "Narita",
...   },
...   "hipsters": {
...      "London": "Soho"
...   }
... }
>>> 
>>> if "airport" in my_dict:
...      result = my_dict["airport"].values()
... else:
...      result = []
... 
>>> print result
['Heathrow', 'Narita']
>>> 

      

+3


source


What I would like to avoid, if possible, is to go through all the dictionary keys and filter them out.

Why? Why do you think Python does the equivalent of a full scan on the database? Dictionary filtering does not mean sequential scanning.

Python:

[value for  key, value in my_dict.items() if key[0] == "airport"]

      

Output:

['Narita', 'Heathrow']

      

+1


source







All Articles