A variety of flake8 fixes.

F401 module imported but unused
F812 list comprehension redefines variable
F821 undefined name
F841 local variable assigned to but never used
E711 comparison to None should be ‘if cond is None:’
E713 test for membership should be ‘not in’