If you have a rack, and are carrying just the lock, here's what you can do:
Open the lock, slip a nice thick rubber band around the shackle, then close the lock.
Hold the lock with the U part pointing down, and slip it between the outer rail of the rack (where the pannier hooks would sit), and the main bed of the rack. The thick part of the lock won't fit through this space, so it will rest there.
Now loop the rubber band over the lower hook (where the bungie of the pannier would attach). This will hold it in place to prevent rattling. The rack struts will keep the lock away from the wheel.
This works with most normal racks (Blackburn, etc). If you have a Tubus Fly or something, I guess you're out of luck.