How to retreive values from list: python
Question: **Summary:** I have a list

    
    
    list = [
            {
                'id': '1',
                'elements': 'A',
                'table': 'maps/partials/a.html',
                'chart': 'maps/partials/charts/a.html',
            },
            {
                'id': '2',
                'elements': 'B',
                'table': 'maps/partials/census/b.html',
                'chart': 'maps/partials/charts/b.html',
            },
            {
                'id': '3',
                'elements': ('C','D','E','F'), //i believe it is wrong 
                'table': 'maps/partials/census/common.html',
                'chart': 'maps/partials/charts/common.html',
            },]
    some_arr = ['E','2011','English','Total']
    

I want to compare `elements` with items in `some_arr`. and i am doing this to
get list and compare it with some_arr.

    
    
    for data in list:
                for i in range(len(some_arr)):
                    if data['elements'] == some_arr[i]:
                        print(data['id'])
    

As you can see in `'id':3` section_title has 4 values. How do i compare
elements here with some_arr.


Answer: If you want to find any matches:

    
    
    lst = [
            {
                'id': '1',
                'elements': 'A',
                'table': 'maps/partials/a.html',
                'chart': 'maps/partials/charts/a.html',
            },
            {
                'id': '2',
                'elements': 'B',
                'table': 'maps/partials/census/b.html',
                'chart': 'maps/partials/charts/b.html',
            },
            {
                'id': '3',
                'elements': ('C','D','E','F'),
                'table': 'maps/partials/census/common.html',
                'chart': 'maps/partials/charts/common.html',
            }]
    some_arr = ['E','2011','English','Total']
    st = set(some_arr)
    
    from collections import Iterable
    for d in lst:
        val = d["elements"]
        if isinstance(val, Iterable) and not isinstance(val, str):
            if any(ele in st for ele in val):
                print(d["id"])
        else:
            if val in st:
                print(d["id"])
    

Making a set of all the elements in `some_arr` will give you `O(1)` lookups,
using `isinstance(val, Iterable) and not isinstance(val, str)` will catch any
iterable value like a list, tuple etc.. and avoid iterating over a string
which could give you false positives as `"F"` is in `"Foo"`.
[any](https://docs.python.org/3.4/library/functions.html#any) will _short
circuit_ on the first match so if you actually want to print id for every
match then use a for loop. Lastly if you are using python2 use `basestring`
instead of `str`.

