As I understand it, original code posted as part of answers to SO are licensed under a Creative Commons license. This addresses the question of whether copying is allowed (i.e. properly attributed, yes).
Now, as for whether this is a reasonable way to answer a question? Sure, I don't see why not. Taking as granted that the question really is different, but code found in another question (either in the question itself or in one of its answers) is still applicable and useful, not only is it fine to copy the code, it would be silly not to.
Of course, you should be very careful when you do this. It seems to me that being able to copy/paste code from another question is a strong indicator that you are dealing with a duplicate question. Taking the example you describe in your comment above:
"How can I sort these tuples by the value of element 2?" and "How can I sort these strings using their third character?"
It seems to me that those two questions really are the same. I.e. while they are presented using specific examples, the real question is "how can I sort a sequence on some arbitrary key found in the elements of the sequence?"
If we consider those two questions not duplicates of each other, then we should (for example) also consider "how do I add 1 and 1 together?" to be a question different from "how do I add 1 and 2 together?".
Just because the person asking the question wasn't able to generalize their presentation sufficiently, that's no reason for us to have umpteen zillion different instances of answers to the same question. We can safely generalize on behalf of the questioner and resolve those as duplicates, even though the specifics of the question as presented are different.
If you feel the person posting the new question might have trouble generalizing, you can help them out. For example, include a brief comment to help the questioner understand why they are duplicates and how to generalize the answer. Or even better: if one is not already present, add a new answer to the original question in which you rephrase the question as the more generalized form and provide an answer that is more clearly useful (i.e. instead of just doing their work for them, teach them to fish :) ). Then you can add a comment to the new question directing them to that answer specifically.
But all that said, it seems plausible to me that one could run into a situation where the same code is applicable to two genuinely different questions. E.g. where the code is just part of the solution, but is an important and useful part which when combined with some other code that is unique to the answer to the question at hand provides a useful answer to the question.
In that case, copy away. :)
2
?" and "How can I sort these strings using their third character?" Both can be accomplished usingsorted(inputSequence, key = lambda i: i[2])
. The two are similar, but not duplicates. (Obviously, the answer text would have to be modified to suit the question as-asked, but the basic idea is the same.)