import ast
my_code = """
print "Hello"
y = [x ** 2 for x in xrange(30)]
"""
module = ast.parse(my_code)
for node in ast.walk(module):
    if type(node) == ast.ListComp:
        print(node.lineno)
print(node.col_offset)
print(node.elt)
How to find list comprehension in python code