Sometimes there is a need to search for data, and if that dictionary is in a list of dictionary items, how can you easily check if that item exists within a given list of dictionaries? Enter the built-in function called any.
Python's 2.7.x help describes any as:
Return True if any element of the iterable is true. If the iterable is empty, return False.Any it is a short cut for:
Now let's use the function. Assume we have a given list, aListofDicts, that contains n number of dictionaries defined as such:
To search this data, one would off the cuff have to write the above function to find the value in an iterable, but any gives us the magic shortcut because no one wants to write long scripts.
So what happened in the above code? We used generator expression, which is just a big word that creates an object that we can iterate on, ie a list. That is this part:
Next we appends a conditional to store a list of boolean values (True/False)
This gives you a list of True/False values instead of a list of the actual dictionary or dictionary values. This means if you have 1 million items in your list, you will have 1 million True/False values. Not very efficient.
Now enters the any function:
exists = any([d['alpha'] == "As soon as the list hit that True, if it's in position 1 or position 1 million of the list, it will stop the iteration, thus saving time and memory. Hooray!
Happy Coding!