How to read and write n number of GPIO's

(deleted)

I am not sure exactly what you mean but I am using an MCP23017 to read multiple inputs (buttons) on an arduino nano using only the 2 wires plus ground and +5v pins of the nano. My code is on the Boat automation project post in this forum.

I understand your concept,Here you used I/O extender but it needs individual wire to read input.

So it would seem you don’t understand the concept, or I am not understanding what you are saying.

But in my case i dont have enough space to use individual wire for all Inputs.

For an N by M matrix giving N*M keys, you only need N+M wires connected to port expanders and they are controlled with just two wires from the Arduino.

Yes, there are only 2 wires connected to the arduino. I currently have two mcp23017 ICs connected to my arduino but still only 2 wires. Of course you still need connections between the ICs and your buttons but this is the case no matter your approach I would have thought.