Instead of iterating through all objects, you can iterate through the items in a specific collection:
import bpy
# Name of the collection to inspect
collection_name = "TestCollection"
collection = bpy.data.collections.get(collection_name)
if collection is None:
print(f"Collection '{collection_name}' not found.")
else:
print(f"Objects in collection '{collection_name}':")
for obj in collection.objects:
print(f"- {obj.name}")
Printing to the console:

For this sample workspace that contains a torus and sphere with the default names
