Python - How to find an item exists in a list (or sublist)?
Question: For example

    
    
    my_list = [1, 2, 3, 4, [5, 6], [7, 8]]
    

I want to find if 7 is in `my_list`.? The answer should be True, because it is
part of the last sublist. Any ideas?


Answer: If `mylist` was a list of only lists you could use
`itertools.chain.from_iterable()`

    
    
    import itertools
    mylist = [[1,2,3,4],[5,6],[7,8]]
    merged = list(itertools.chain.from_iterable(mylist))
    7 in merged
    

Since we have a list of ints mixed with lists, we define a custom filter. This
could also be done in a single list comprehension, but I can't figure that out
right now.

    
    
    mylist = [1,2,3,4,[5,6],[7,8]]
    merged = []
    map(lambda l: merged.extend(l) if isinstance(l, list) else merged.append(l), [i for i in mylist])
    7 in merged
    

You could also use the deprecated `compiler.ast.flatten` function:

    
    
    from compiler.ast import flatten
    merged = flatten(mylist)
    

