Can an "almost injective'' function exist between compact connected metric spaces? Let $\pi: X\to Y$ be a surjective continuous function between the compact, metric and connected spaces $X$, $Y$, and $Y_0 = \{y\in Y: \#\pi^{-1}(y)>1\}$. Suppose that:

*

*$Y_0$ is dense in $Y$,

*$Y\setminus Y_0$ is a dense $G_\delta$ in $Y$, and

*for some constant $N<\infty$, we have  $\#\pi^{-1}(y) \leq N$ for all $y\in Y$.

My question is: can a function $\pi$ like this exist? Maybe I need more hypothesis on the topology of $X$ and $Y$.
Observe that the connectedness is crucial: Sturmian codings of irrational rotations of the circle are surjective continuous functions $\pi:K\to S^1$ from the Cantor set (a totally disconnected space) and the circle such that $\#\pi^{-1}(y) = 2$ for $y$ in a countable dense subset $Y_0 \subseteq S^1$ and $\#\pi^{-1}(y) = 1$ for $y \in S^1\setminus Y_0$.
Condition (3) is also needed: if $f\colon[0,1]\to[0,1]$ is the Thomae's function, $X = \{(x,y)\in[0,1]^2 : 0\leq y\leq f(x)\}$ is the subgraph of $f$, $\pi: X\to[0,1]$ is the projection onto the first coordinate, and $Y_0 := \{y\in Y: \#\pi^{-1}(y)>1\} = [0,1]\cap\mathbb{Q}$, then $\pi$ is continuous, $X$ is connected and (1),(2) hold, but $\pi(y)$ is an uncountable set when $y \in Y_0$.
After building these examples I am more convinced than at the beginning that $\pi$ must be injective.
I would appreciate any comment.
 A: 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$.
A: 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):

Now, if $k=1$, you cannot make $T$ injective for Intermediate Value Theorem reasons, and you get attractors that look like this:


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.
A: 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.

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:

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.)
