Potassium channels are exquisitely selective, allowing K+ to pass across cell membranes while blocking other ion types. Here we demonstrate that the number of carbonyl oxygen atoms that surround permeating ions is the most important factor in determining ion selectivity rather than the size of the pore or the strength of the coordinating dipoles. Although the electrostatic properties of the coordinating ligands can lead to Na+ or K+ selectivity at some values of the dipole moment, no significant selectivity arises at the specific value of the dipole moment for carbonyl groups found in potassium channels when the ligands have complete freedom. Rather, we show that the main contribution to selectivity arises from slight constraints on the conformational freedom of the channel protein that limit the number of carbonyl oxygen atoms to a value better suited to K+ than Na+, despite the pore being flexible. This mechanism provides an example of a general framework for explaining ion discrimination in a range of natural and synthetic macromolecules in which selectivity is controlled by the number of coordinating ligands in addition to their dipole moment.