Saad Hasan
Saad Hasan

Reputation: 152

Optimized way to search dictionary of list with the list of elements in Python

I am trying to search for the first occurrence of a dictionary in a list. The logic I created works well for the small data but I have a list of a million rows and the other list elements to search has 1000 elements.

data = [{'team': 'a', 'id':11}, {'team': 'b', 'id':111}, {'team': 'c', 'id':1111}]
list_to_see = ['a','c']
check = None
for index in range(len(data)):
    if data[index]['team'] in list_to_see[index]:
        check = data[index]['id']
        break
print(check)

#Output: 11

So, I got the first element Id which is exactly what I want in the result. Is there any optimized way to get the same output that can handle large data? Thanks

Upvotes: 3

Views: 284

Answers (2)

Saad Hasan
Saad Hasan

Reputation: 152

After playing around with different combinations. I found the solution which is most optimized by converting the list into sets as the implementation of the set in python is based on the Hash table and very fast retrieval of data. It resolved my issue on the real problem I am working on. I am sharing my findings.

import timeit
data = [{'team': 'a', 'id':11}, {'team': 'b', 'id':111}, {'team': 'c', 'id':1111}]
list_to_see = ['a','c']
def test_1():
   check = None
   for index in range(len(data)):
       if data[index]['team'] in list_to_see[index]:
           return data[index]['id']

def test_2():
   g = (i['id'] for i,j in zip(data, list_to_see) if i['team'] in j)
   return next(g)

def test_3():
   for t in data:
       if t['team'] in set(list_to_see):
           return t['id']

if __name__ == '__main__':
   print(timeit.timeit("test_1()", setup="from __main__ import test_1"))
   print(timeit.timeit("test_2()", setup="from __main__ import test_2"))
   print(timeit.timeit("test_3()", setup="from __main__ import test_3"))
#Output
0.5693206999858376
0.8338081999972928
0.4102382999844849

test_3 function where I used sets. The following link helps me to understand set implementations in python: How is set() implemented?. Thank you so much for the support..!

Upvotes: 0

Nk03
Nk03

Reputation: 14949

use generator expression and call next to fetch the 1st value:

g = (i['id'] for i,j in zip(data, list_to_see) if i['team'] in j)
print(next(g))

Upvotes: 5

Related Questions