Praise be to Allah
In the answer to question no. 1200 we explained the prohibition on free mixing and the negative consequences that result from it, especially when it happens among youth in educational settings.
in the answer to questions no. 70223 and 127946 we explained that in countries in which the people are faced with free mixing in most areas of life, especially in educational institutions and work places, there is a concession granted to them in that regard which is not granted to others, based on the fiqhi principle that “whatever is prohibited so as to bar the means that may lead to evil may be permitted in cases of necessity and where doing so serves a greater interest.”
However care should be taken to guard one’s tongue, lower one’s gaze and avoid anything that may be a cause of temptation.
This is in general terms, in places where this problem is widespread and the Muslim is not able to change it. But in specific terms, if a Muslim is able to enrol in a school in which there is no mixing, or in an education centre or private classes in which the genders are separated, then he must enrol in such classes, so as to avoid trouble and ward off temptation, and so as to adhere to the limits set by Allah, may He be exalted.
But if that is not available, or it is available to some extent but there is a great difference in the educational level between the two options, and he fears that he may be adversely affected or may miss out on some benefits because of that, then in that case there is a concession allowing him to enrol in that which is more beneficial to him, even if there is free mixing in that place. However it is essential for him to denounce the evil as much as he is able to, and to adhere to his religious commitment and good manners, by remembering that Allah, may He be exalted, is always watching. So he should guard his tongue, lower his gaze and avoid as much as possible places where there is mingling and direct interaction.
Shaykh Ibn ‘Uthaymeen (may Allah have mercy on him) was asked: What is the ruling on a woman going to a male doctor to treat her, when there is a female doctor in the same specialty?
If the specialty is the same, and the man and the woman are equal in terms of their skill level, then a woman should not go to the male doctor, because there is no need to do so.
But if the man is more skilled than the woman, or more specialised, then there is nothing wrong with the woman going to him, even if there is a female doctor, because this is a case of need, and in case of need such things are permissible.
End quote from Fataawa ‘Ulama’ al-Balad al-Haraam (p. 693)
But if he is a person who fears that he may be tempted and fall into sin, then he does not have the right to do that, because preserving one’s religious commitment takes precedence over all other interests. In that case he should leave these educational centres for the sake of Allah, and enrol in a place that pays attention to separating the sexes, even if its level of education is lower. Then he should strive hard in his studies and think positively of his Lord, when he refrains from attending the mixed institution for the sake of Allah, for whoever gives up something for the sake of Allah, Allah will compensate him with something that is better for him than it.
Please see the answer to question no. 50398
For more information, please see also the answer to question no. 128996
And Allah knows best.