Friday, March 21, 2014

Searching a List of Dictionaries

Often when I work in Python, I get JSON text which I convert using the JSON module in python.  This is great for me because I now have lots of data in dictionaries and lists.  Dictionaries are easy to use because everything is in a Key/Value format.  I view it as the data has nice neat labels.

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:
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False 

Now let's use the function.  Assume we have a given list, aListofDicts, that contains n number of dictionaries defined as such:
aListofDicts = [{"alpha": "a"}, {"alpha": "b"},
               {"alpha": "joker"},{"alpha": 1.2}]

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.
>>> print any(d['alpha'].lower() == 'a'.lower() for d in aListofDicts)
>>> print any(d['alpha'].lower() == 'tank'.lower() for d in aListofDicts)

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:
aListofValues = [d for d in aListofDicts]
 Next we appends a conditional to store a list of boolean values (True/False)
aListofValues = [d[''] == "
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!

1 comment:

darshana patel said...

It’s really very important information on GIS Technology and for our modern world, a geographic information system (GIS) lets us visualize, question, analyze, interpret, and understand data to reveal relationships, patterns, and trends. GIS Company in India , GIS consulting firms, Thank you so much for your valuable time to write this important information on GIS.