Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is there a way to efficiently search for a value using 2 keys in python?

Let’s assume I have this table with 3 columns like the one below.
I can make a list of values and simply iterate through every row but that seems inefficient to my rookie eye if there is a lot of data, and it doesn’t seem scalable if applied in large projects:

array = [
    ['a', 'x', 100],
    ['a', 'y', 200],
    ['a', 'z', 300],
    ['b', 'x', 150],
    ['b', 'y', 1000],
    ['b', 'z', 50],
    ['c', 'x', 790],
    ['c', 'y', 456],
    ['c', 'z', 500]
]

I was thinking of using nested dictionaries, which look more efficient, but are not exactly scalable:

table = {
    'a': {'x': '100', 'y': '200', 'z': '300'},
    'b': {'x': '150', 'y': '1000', 'z': '50'},
    'c': {'x': '790', 'y': '456', 'z': '500'}
}

Is there a more efficient way to search for these values? Like indexing on SQL based on two columns.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can use a dictionary where the keys are 2-tuples and the values are integers:

{(fst, snd): thrd for fst, snd, thrd in array}

This outputs:

{
 ('a', 'x'): 100,
 ('a', 'y'): 200,
 ('a', 'z'): 300,
 ('b', 'x'): 150,
 ('b', 'y'): 1000,
 ('b', 'z'): 50,
 ('c', 'x'): 790,
 ('c', 'y'): 456,
 ('c', 'z'): 500
}

which allows for O(1) expected time for lookup.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading