26

Yesterday I posted this question, but after a few minutes I figured out the solution and answered it myself.

The question is being downvoted, even though it doesn't seem so bad to me, but maybe I'm missing something obvious. The weird thing, however, is that my answer is being downvoted as well, even if it apparently works. I edited the answer to ask for an explanation for the downvotes, but my edit has been eliminated.

Now, I really don't care about the downvotes, I was just asking for explanations because I was afraid my solution - which seems ok to me - is actually not the proper way to solve my problem. Is this the case? Or is it being downvoted for some other reason? I don't know, maybe it is considered...rude to answer your own question after a short time? :/

2
  • 1
    so, your problem is effectively... you miss-used a language construct because you thought it did X, when it actually did Y. I can't necessarily answer for why the people who downvoted downvoted, but ultimately your question is a valid "How do I do X" question, with an attempt that had what some would consider a silly mistake. I wouldn't take the downvotes alone as evidence that there's a problem with your solution.
    – Kevin B
    Commented Mar 4, 2022 at 16:14
  • 25
    The right place to ask for how to improve your post is here on meta. Do not add commentary to your posts asking for explanation of downvotes as this is inappropriate and you will never get a reply this way.
    – Dharman Mod
    Commented Mar 4, 2022 at 16:14

2 Answers 2

47

The core issue of the question is "how to iterate over the values of a dictionary". That by itself would neatly fit into a title, a mre would be a few lines of code, and there is one obvious correct answer.

The question

The title "Extracting a list from a nested Python dictionary using list comprehension" does not accurately reflect what the question is about. The question is not actually about extracting a list, the nesting is only incidentally relevant, and what remains is rather generic (albeit accurate).
Someone with the same issue is unlikely to realise it based on the title.

The body needlessly avoids asking a proper debugging question. Both the input and output are styled as some abstract pseudo-values – wrong syntax, undefined placeholders – when it would be trivial to modify to provide both correctly.

# abstract pseudo-values
[value_00, value_01, ... value_N]
# concrete values
["value_00", "value_01", "value_N"]

The question contains an attempt at solving the task, yet not at solving the problem. Merely following the information of the error – that there is a problem with indexing – would have given an insight into what goes wrong, even if not how to fix it.

# an attempt to solve the task
result = [obj['detection']['value'] for obj in dic]
# an attempt to solve the problem
result = [obj for obj in dic]

The answer

The answer is almost correct, in the sense that it works but is exactly what one should not teach to others: it correctly identifies the issue (one needs the values instead of the keys) but presents a suboptimal solution.
Now, of course not every answer has to be perfect. However, there is a method literally named as the desired thing. Searching the keywords gives multiple appropriate solutions, even when not restricted to Stack Overflow.


In short, the question seems not useful for searching and the answer not useful for solving the problem.

26

Well, I had only stumbled upon that question because of the complaint about the downvotes. But a deeper inspection makes it clear that the question emerged due to a misunderstanding of how iterating a dictionary in a for loop works: unless you do .values() or .items(), it gives you the keys of the dictionary rather than the values within. Other questions in the platform already cover this confusion fairly well, including this one, which this question was now voted to close as a duplicate of.

Overall, it is very likely that other viewers of that Python question found that such a mistake does not make a useful contribution to the site. The usual way to prevent this is to do more research effort prior to asking, which would probably have led you to the linked question or some other resource explaining how to iterate over dictionaries in Python.

Moreover, there is usually no issue in answering your own question (see this though), but that alone does not make it exempt from downvotes.

See also:

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .