It sort of was - it was also the love that Harry felt.
The Dark Lord was able to possess Harry successfully at first, but was driven out when Harry started thinking of Sirius, his godfather who he loved and was eager to see again if he died.
“Let the pain stop, thought Harry … let him kill us … end it, Dumbledore … death is nothing compared to this …
And I’ll see Sirius again …
And as Harry’s heart filled with emotion, the creature’s coils loosened, the pain was gone; Harry was lying face down on the floor, his glasses gone, shivering as though he lay upon ice, not wood …”
- Harry Potter and the Order of the Phoenix, Chapter 36 (The Only One He Ever Feared)
Dumbledore explains this to Harry as well, that it was love, specifically the love inside of Harry, that saved him from being possessed by the Dark Lord.
“It is the power held within that room that you possess in such quantities and which Voldemort has not at all. That power took you to save Sirius tonight. That power also saved you from possession by Voldemort, because he could not bear to reside in a body so full of the force he detests. In the end, it mattered not that you could not close your mind. It was your heart that saved you.”
- Harry Potter and the Order of the Phoenix, Chapter 37 (The Lost Prophecy)
He also says to Harry that there’s power in an untarnished and whole soul.
“You have flitted into Lord Voldemort’s mind without damage to yourself, but he cannot possess you without enduring mortal agony, as he discovered in the Ministry. I do not think he understands why, Harry, but he was in such a hurry to mutilate his own soul, he never paused to understand the incomparable power of a soul that is untarnished and whole.”
- Harry Potter and the Half-Blood Prince, Chapter 23 (Horcruxes)
Dumbledore also explains to Snape that it’s the close contact with Harry’s mind and soul that caused the Dark Lord that pain. This time, he mentions souls as well, and says that the Dark Lord’s maimed soul couldn’t bear close contact with a soul like Harry’s.
“Voldemort fears that connection,’ said Dumbledore. ‘Not so long ago he had one, small taste of what truly sharing Harry’s mind means to him. It was pain such as he has never experienced. He will not try to possess Harry again, I am sure of it. Not in that way.’
‘I don’t understand.’
‘Lord Voldemort’s soul, maimed as it is, cannot bear close contact with a soul like Harry’s. Like a tongue on frozen steel, like flesh in flame –”
- Harry Potter and the Deathly Hallows, Chapter 33 (The Prince’s Tale)
Every time Dumbledore discussed why the Dark Lord couldn’t possess Harry, it’s about Harry himself. This also makes sense - the Dark Lord could possess Nagini, who was a Horcrux so also contained a bit of his soul, without pain.
“You seem to have visited the snake’s mind because that was where the Dark Lord was at that particular moment,’ snarled Snape. ‘He was possessing the snake at the time and so you dreamed you were inside it, too.”
- Harry Potter and the Order of the Phoenix, Chapter 24 (Occlumency)
The difference is Harry himself - it’s not the soul fragments sharing space.
In addition, only remorse can heal a torn soul - not proximity.
The only known way to repair a torn soul and put the pieces back together is by feeling remorse.
“Isn’t there any way of putting yourself back together?’ Ron asked.
‘Yes,’ said Hermione, with a hollow smile, ‘but it would be excruciatingly painful.’
‘Why? How do you do it?’ asked Harry.
‘Remorse,’ said Hermione. ‘You’ve got to really feel what you’ve done. There’s a footnote. Apparently the pain of it can destroy you.”
- Harry Potter and the Deathly Hallows, Chapter 36 (The Flaw in the Plan)
If two soul fragments sharing a container put them back together, the Dark Lord couldn’t possess Nagini without ending up unmaking her as a Horcrux.