Can an "almost injective'' function exist between compact connected metric spaces?

Let $f$ be the complex polynomial $f(z) = z^2 - 1$. Its Julia set $J(f)$ is the set of non-equicontinuity of the iterates; i.e., the set of points whose orbit under $f$ is not stable under perturbation. Equivalently, it is the boundary of the set of points whose iterates do not tend to infinity under iteration.

For this particular function, the Julia set is called the "basilica"; it looks like this. Basilica Julia set

This Julia set can be described as the quotient of the unit circle by a certain equivalence relation, which is encoded by the following picture, called a lamination: Basilica lamination

Here points are identified if they are the endpoints of the same "leaf", i.e. hyperbolic segment containing two points. These points correspond exactly to the "pinchings" in the original Julia set. They are clearly dense in the circle, but the corresponding points in the Julia set are all preimages of a single fixed point (shown in yellow on the picture above), so this set is countable. Hence, taking $X=S^1$ and $Y=J(f)$, you obtain an example satisfying your desired properties. The function $\pi$ is precisely the continuous extension of the Riemann map from the complement of the closed unit disc to the unbounded complementary component of $J(f)$.

(Images from Wikimedia commons, https://commons.wikimedia.org/wiki/File:Julia_z2-1.png by user Prokofiev and https://commons.wikimedia.org/wiki/File:Basilica_lamination.png by user adam majewsi.)


A simple example is the Peano curve $f:[0,1]\to[0,1]^2$.

Specifically, here $Y_0=([0,1]\times T)\cup (T\times [0,1])$ where $T$ is the set of triadic rationals in $[0,1]$, a dense $F_\sigma$ with empty interior, so $Y\setminus Y_0=T^c\times T^c$ is a dense $G_\delta$. Check here for details. Note that here $N=4$ and, as per Anton Petrunin's remark in a comment to the linked answer, one could modify the construction to make $N=3$.


Not a definite answer, but I have to join images. Since it needs some patience to get to my point, let me say that I suspects that examples verifying (1), (2) and (3) do exist.

Since you mention a dynamical example, you might be familiar with the solenoid $$ S = \{(x_{-n})_{n\in\mathbb{N}} \mid x_{i+1} = 2 x_i\} \subset (\mathbb{R}/\mathbb{Z})^\mathbb{N}$$ Derived from expanding maps where introduced by Smale as example of hyperbolic behavior; they are maps $T: (\mathbb{R}/\mathbb{Z}) \times \mathbb{D}^k$ of the form $$ T(x,y) = (2x, R(x,y))$$ where $\partial_y R$ has operator norm everywhere less than $1$, implying that $T$ has a compact attractor $\Lambda$. If $k>1$, one can arrange $R$ so that $T$ is a diffeomorphism onto its image. It then realizes a homeomorphism of $\Lambda$, which is homeomorphic to the solenoid $S$, as pictured here (by Jim.belk): Smale Solenoid

Now, if $k=1$, you cannot make $T$ injective for Intermediate Value Theorem reasons, and you get attractors that look like this: Weird attractor Another weird attractor You still have a map $S\to\Lambda$, obtained by seeing the $k=1$ example as a projection from a $k=2$ injective example. I suspects that this map could be an example of the kind you are seeking. A proof could be uneasy, but at least I feel you might take inspiration from this to design a simpler example, from a solenoid to the plane, as a limit of maps from the circle with more and more self-intersections. It seems that (3) is the less obvious to ensure, but generically we do not expect any triple intersection.