0

So basically I have this:

data = [
    {
        "id" : "hello",
        ...
    },
    {
        "id" : "world",
         ...
    }
]

and I want to get the result:

"1" 

if I enter something like:

get_index(id="world")

And the main thing is that I don't want to loop through the array since there is a lot of data in it. If I were to loop it would be easy and I would end up with some code like:

for obj in data:
    if obj['id'] == 'hello':
        print(obj['id'])

But is there a direct way of doing this without looping?

mrCarnivore
  • 4,638
  • 2
  • 12
  • 29
ICanKindOfCode
  • 1,000
  • 1
  • 11
  • 32
  • If the IDs being sorted alphabetically isn't just a coincidence of the example you could use a bisection search. But in general: no, this will be a O(n) operation unless you can exploit some additional information about the structure of your data. – jonrsharpe Dec 08 '17 at 14:49
  • Ok thanks. Could you just say that in an answer so I could term it as accepted? – ICanKindOfCode Dec 08 '17 at 14:50
  • 1
    Well in this particular use case, it easy for you to say _I don't want to loop because there is a lot of data._ But do you actually know the exact position of the information you're looking for? Probably not. You can optimize the search using appropriate data structures like sets if possible. But you're asking for a solution to search item without actually searching. This is not posssible. – scharette Dec 08 '17 at 14:50
  • The main issue is that I have no control over this data. I am being provided by someone else. Otherwise I wold just include an easy solution... – ICanKindOfCode Dec 08 '17 at 14:52

2 Answers2

1

It is not possible to do this more efficiently than the looping you described as long as you can't exploit the specific structure of the data in some way.

mrCarnivore
  • 4,638
  • 2
  • 12
  • 29
  • How is this any different than what comments says. I don't see the added value of your answer. Especially since it does not provide any insight on how to acheive what the PO wanted. – scharette Dec 08 '17 at 15:12
  • The author asked if there is a direct way and there is not. Hence my answer is answering the question... – mrCarnivore Dec 08 '17 at 15:19
  • @scharette This is a legitimate question and the answer certainly has value. It's very frustrating coming from a javascript background to learn that you can access lists and dicts directly. – Jamie_D Dec 14 '22 at 17:57
0

You'll have to at least go through the whole array once.

However, if you need to search for many different keys in the array, you could build an index:

>>> data = [
...     {
...         "id" : "hello",
...     },
...     {
...         "id" : "world",
...     }
... ]
>>> get_index= {item["id"]:counter for (counter, item) in enumerate(data)}
>>> get_index["hello"]
0
>>> get_index["world"]
1
>>>

As you now have a lookup table, queries to ids should now be constant time operations: How expensive are Python dictionaries to handle?

Lanting
  • 3,060
  • 12
  • 28