Cycling between cameras through python script issue cameravariable = "cameraname"

list3=bpy.data.cameras.keys() 
regex3=re.compile(".*"+cameravariable+".*") 
cam=[m.group(0) for l in list3 for m in [regex3.search(l)] if m]

for c in cam :
bpy.context.scene.camera = bpy.data.objects[c]
print("Camera change")


Hey, this is how I access my cameras in my scene and cycle through them, but right now it's looking at the data block name, not the camera object. Which is annoying since sometimes my script will try to make a camera that is in another scene the camera for rendering, which in turn just fail my script. So I have to be careful when copying cameras accross scenes to make sure I rename both the object and the data block, which I tend to forget.

So how can I base my script on the camera objects? Or perhaps more simply how I cycle the cameras INSIDE my scene?
You can access all objects of the current scene by using bpy.context.scene.collection.all_objects. Then you can filter the objects by their type like lemon suggested, list comprehension makes the code a little shorter. Once you have a list of the cameras you can iterate through them and activate one after another.

import bpy

obj_in_scene = bpy.context.scene.collection.all_objects
cameras = [obj for obj in obj_in_scene if obj.type == 'CAMERA']

for cam in cameras:
    bpy.context.scene.camera = cam
    # Do something else here while this object is the current camera
    print(bpy.context.scene.camera.name)