What is it?

The XY problem is asking about your attempted solution rather than your actual problem.

That is, you are trying to solve problem X, and you think solution Y would work, but instead of asking about X when you run into trouble, you ask about Y.

The problem

This can lead to frustration by people who are trying to help you solve the problem because by the time you ask about it, the solution that you need help with might not have any obvious connections to the problem that you are trying to solve.

How to avoid it

To avoid falling into this trap, always include information about a broader picture along with any attempted solution. If someone asks for more information, or especially a more specific question, do provide details. If there are other solutions which you believe will be suggested and which you've already ruled out, then don't try to avoid going over them again – instead state why you've ruled them out, as this gives more information about your requirements and helps others provide better answers.

An example

A recent IRC conversation for illustration:

Q: Is there a function to return a string between two delimiters? B: I don't understand what you mean, but I doubt there's already a function C: Split and slice D: Partition too Q: I tried partition

Q: I was trying to use built-ins to get the number between something like this in a string "attribute1: 50.223, attribute2: 442.1" D: Why not just parse the string? Q: I thought there may have been some built in parsing stuff D: pairs = [x.strip() for x in s.split(",")]; attribs = {k: v for x in pairs for k, v in [x.split(": ")]}

D: There's a few libraries, but simplistic formats are easy enough -- if you don't care about error handling

D: Changing the source to use a well known format, e.g. json or yaml, is preferred when possible Q: This code actually comes from HTML

Q: But I don't know how to parse Javascript with HTMLParser or whatever it's called D: Is it merely embedded in HTML, or some mangled version of HTML? Q: It's embedded in the HTML D: If it's JavaScript (and that is, except for missing outer braces), JSON can probably parse it Q: Thanks D: I didn't say it explicitly: JSON only parses data structures, not JS code Q: That's all I need parsed is a data structure

The problem is really about how to parse JavaScript data structures, not find "a string between two delimiters", yet it takes quite a bit of time and intuition to get to the real issue.

This is easier to do in a fully interactive chat (regardless of what mode), but on a SE site, where you polish a post a bit, post it, and then have 5-30 mins, or longer, before feedback, it really helps to head in the right direction from the start.