efficient way to search a list of dictionaries in python

  • Last Update :
  • Techknowledgy :

The following code uses the next() function to search a list of dictionaries in Python.

lstdict = [{
      "name": "Klaus",
      "age": 32
   },
   {
      "name": "Elijah",
      "age": 33
   },
   {
      "name": "Kol",
      "age": 28
   },
   {
      "name": "Stefan",
      "age": 8
   }
]
print(next(x
   for x in lstdict
   if x["name"] == "Klaus"))
print(next(x
   for x in lstdict
   if x["name"] == "David"))

Output:

{'name': 'Klaus', 'age': 32}
Traceback (most recent call last):
  File "<string>", line 8, in <module>
StopIteration

However, this problem can be easily treated in the code above. You simply tweak and provide a default with the use of a slightly different API.

lstdict = [{
      "name": "Klaus",
      "age": 32
   },
   {
      "name": "Elijah",
      "age": 33
   },
   {
      "name": "Kol",
      "age": 28
   },
   {
      "name": "Stefan",
      "age": 8
   }
]
print(next((x
   for x in lstdict
   if x["name"] == "David"), None))
7._
listOfDicts = [{
      "name": "Tommy",
      "age": 20
   },
   {
      "name": "Markus",
      "age": 25
   },
   {
      "name": "Pamela",
      "age": 27
   },
   {
      "name": "Richard",
      "age": 22
   }
]
list(filter(lambda item: item['name'] == 'Richard', listOfDicts))

The following code uses list comprehension to search a list of dictionaries in Python.

lstdict = [{
      "name": "Klaus",
      "age": 32
   },
   {
      "name": "Elijah",
      "age": 33
   },
   {
      "name": "Kol",
      "age": 28
   },
   {
      "name": "Stefan",
      "age": 8
   }
]

print([x
   for x in lstdict
   if x['name'] == 'Klaus'
][0])

Suggestion : 2

Sometimes you may need to look for an item in a list of dictionaries in python. In this article, we will look at couple of ways to search item in list of dictionaries in Python.,Here are couple of ways to search item in list of dictionaries in python. Let us say you have the following dictionary.,How to Remove Multiple Items from List in PythonHow to Flatten List of Dictionaries in PythonHow to Flatten List of Tuples in PythonHow to Find & Delete Broken SymlinksHow to Remove Duplicates from List in Python,The most basic way to search an item based on key, value combination is to loop through each item and return the dictionary once it is found. Here is an example where we look for dictionary containing name = Jim

Here are couple of ways to search item in list of dictionaries in python. Let us say you have the following dictionary.

>>> data = [{
      'name': 'Joe',
      'age': 20
   }, {
      'name': 'Tim',
      'age': 25
   }, {
      'name': 'Jim',
      'age': 30
   }] >>>
   data[{
      'age': 20,
      'name': 'Joe'
   }, {
      'age': 25,
      'name': 'Tim'
   }, {
      'age': 30,
      'name': 'Jim'
   }]

The most basic way to search an item based on key, value combination is to loop through each item and return the dictionary once it is found. Here is an example where we look for dictionary containing name = Jim

>>>
for i in data:
   if i['name'] == 'Jim':
   print i

{
   'age': 30,
   'name': 'Jim'
}

You can also achieve the above output using dictionary comprehension and next() function.

>>> res = next((sub
      for sub in data
      if sub['name'] == 'Jim'), None) >>>
   res {
      'age': 30,
      'name': 'Jim'
   }

Suggestion : 3

Last Updated : 18 Aug, 2022

Output :

The filtered dictionary value is: {
   'Course': 'Python',
   'Author': 'Mark'
}

Output:

The filtered dictionary value is: {
   'Course': 'Java',
   'Author': 'Paul'
}

Suggestion : 4

Short answer: The list comprehension statement [x for x in lst if condition(x)] creates a new list of dictionaries that meet the condition. All dictionaries in lst that don’t meet the condition are filtered out. You can define your own condition on list element x.,Solution: Use list comprehension [x for x in lst if condition(x)] to create a new list of dictionaries that meet the condition. All dictionaries in lst that don’t meet the condition are filtered out. You can define your own condition on list element x. ,In this article, you’ve learned how to filter a list of dictionaries easily with a simple list comprehension statement. That’s far more efficient than using the filter() method proposed in many other blog tutorials. Guido, the creator of Python, hated the filter() function!,The output is the filtered list of dictionaries that meet the condition:

Here’s a quick and minimal example:

l = [{
   'key': 10
}, {
   'key': 4
}, {
   'key': 8
}]

def condition(dic):
   ''
' Define your own condition here'
''
return dic['key'] > 7

filtered = [d
   for d in l
   if condition(d)
]

print(filtered)
#[{
   'key': 10
}, {
   'key': 8
}]

Minimal Example: Consider the following example where you’ve three user dictionaries with username, age, and play_time keys. You want to get a list of all users that meet a certain condition such as play_time>100. Here’s what you try to accomplish:

users = [{
      'username': 'alice',
      'age': 23,
      'play_time': 101
   },
   {
      'username': 'bob',
      'age': 31,
      'play_time': 88
   },
   {
      'username': 'ann',
      'age': 25,
      'play_time': 121
   },
]

superplayers = # Filtering Magic Here

print(superplayers)

The output should look like this where the play_time attribute determines whether a dictionary passes the filter or not, i.e., play_time>100:

[{
      'username': 'alice',
      'age': 23,
      'play_time': 101
   },
   {
      'username': 'ann',
      'age': 25,
      'play_time': 121
   }
]

The output is the filtered list of dictionaries that meet the condition:

[{
      'username': 'alice',
      'age': 23,
      'play_time': 101
   },
   {
      'username': 'ann',
      'age': 25,
      'play_time': 121
   }
]

Minimal Example: Consider the following example again where you’ve three user dictionaries with username, age, and play_time keys. You want to get a list of all users for which the key play_time exists. Here’s what you try to accomplish:

users = [{
      'username': 'alice',
      'age': 23,
      'play_time': 101
   },
   {
      'username': 'bob',
      'age': 31,
      'play_time': 88
   },
   {
      'username': 'ann',
      'age': 25
   },
]

superplayers = # Filtering Magic Here

print(superplayers)

Suggestion : 5

DictionaryKeys (last edited 2022-01-21 13:47:38 by eriky)

   1 # retrieve the value
   for a particular key
   2 value = d[key]
   1 def lookup(d, key):
      2 ''
   'dictionary lookup is done in three steps:
   3 1. A hash value of the key is computed using a hash
   function.
   4
   5 2. The hash value addresses a location in d.data which is
   6 supposed to be an array of "buckets"
   or "collision lists"
   7 which contain the(key, value) pairs.
   8
   9 3. The collision list addressed by the hash value is searched
   10 sequentially until a pair is found with pair[0] == key.The
   11
   return value of the lookup is then pair[1].
   12 ''
   '
   13 h = hash(key) # step 1
   14 cl = d.data[h] # step 2
   15
   for pair in cl: # step 3
   16
   if key == pair[0]:
      17
   return pair[1]
   18
   else:
      19 raise KeyError, "Key %s not found." % key
1._
   1 # retrieve the value
   for a particular key
   2 value = d[key]
2._
   1 def lookup(d, key):
      2 ''
   'dictionary lookup is done in three steps:
   3 1. A hash value of the key is computed using a hash
   function.
   4
   5 2. The hash value addresses a location in d.data which is
   6 supposed to be an array of "buckets"
   or "collision lists"
   7 which contain the(key, value) pairs.
   8
   9 3. The collision list addressed by the hash value is searched
   10 sequentially until a pair is found with pair[0] == key.The
   11
   return value of the lookup is then pair[1].
   12 ''
   '
   13 h = hash(key) # step 1
   14 cl = d.data[h] # step 2
   15
   for pair in cl: # step 3
   16
   if key == pair[0]:
      17
   return pair[1]
   18
   else:
      19 raise KeyError, "Key %s not found." % key

For such a lookup algorithm to work correctly, the hash functions provided must guarantee that if two keys produce different hash values then the two key objects are not equivalent, that is,

for all i1, i2,
if hash(i1) != hash(i2), then i1 != i2

Note that this function meets the requirements of a hash function - every time two keys have different hash values, they represent different objects. (This is trivially true because no keys have different hash values - they all have the value 1.) But this is a bad hash function because it means that all (key, value) pairs will be placed in a single list, and so each lookup will require searching this entire list. Thus a (very) desirable property of a hash function is that if two keys produce the same hash values, then the key objects are equivalent, that is,

for all i1, i2,
if hash(i1) == hash(i2), then i1 == i2
6._
   1 >>> l = [1, 2]
   2 >>> d = {}
   3 >>> d[l] = 42
   4 >>> l.append(3)
   5 >>> d[l]
   6 Traceback (most recent call last):
   7   File "<interactive input>", line 1, in ?
   8 KeyError: [1, 2, 3]
   9 >>> d[[1, 2]]
  10 Traceback (most recent call last):
  11   File "<interactive input>", line 1, in ?
  12 KeyError: [1, 2]
   1 def hash(obj):
      2
   return 1
   1 >>> l = [1, 2]
   2 >>> d = {}
   3 >>> d[l] = 42
   4 >>> l.append(3)
   5 >>> d[l]
   6 Traceback (most recent call last):
   7   File "<interactive input>", line 1, in ?
   8 KeyError: [1, 2, 3]
   9 >>> d[[1, 2]]
  10 Traceback (most recent call last):
  11   File "<interactive input>", line 1, in ?
  12 KeyError: [1, 2]