Home > Software design >  Is there a way to efficiently search for a value using 2 keys in python?
Is there a way to efficiently search for a value using 2 keys in python?

Time:05-19

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.

CodePudding user response:

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.

  • Related