Your answer is actually wrong.
If your active collection is another as the main collection, when running your script the object will not be removed from this.
import bpy
bpy.ops.mesh.primitive_cube_add()
# our created cube is the active one
obj = bpy.context.active_object
# removeRemove itobject from all collections not used in thea scene
bpy.ops.collection.objects_remove_all()
# add it to our specific collection
bpy.data.collections['MyCollection'].objects.link(obj)
About remove all: https://docs.blender.org/api/blender2.8/bpy.ops.collection.html#bpy.ops.collection.objects_remove_all