How to set visible render layers with Python?
My code may be awful (abeit easily readable), but I think the problem you have is this: it's impossible to turn all layers off. One must be turned on, so a loop to disable them all doesn't work. It seems that Blender is checking anytime you try to disable a layer whether it's the last man standing.

I simply turned it around by first switching the desired layers on and then disabling all others.

import bpy

visible = [0,4]
for b in visible:
    bpy.context.scene.layers[b] = 1

a  = 0
while a &lt; 20:
    if a not in visible:
        bpy.context.scene.layers[a]= 0
    a += 1


edit: batFINGER reduced it to two lines, so give his comment an upvote (: 

import bpy

visible = [3,6,9]
scene.layers = [l in visible for l in range(20)]


Creating the full list before assigning it is a smart move.