If you add RAM to your iMac, you are not limited to using the same size sticks that are already installed. You can add a pair of 16GB sticks, if you like. You do not have to replace your existing sticks, unless you want to do that. The extra 32GB would then give you 40GB total. You can also remove the existing RAM, and swap those out to 16GB sticks, giving you the maximum for your 2017 5K-retina, which is 64GB. But, in my opinion, the more cost-effective upgrade for memory would be to leave the original 2x4GB, and add 2x8GB, which would give you a new total of 24GB.
Also, I suggest an upgrade to Monterey, or Ventura, because your High Sierra system will get less and less useful without updates.