-1

What should be the correct way of writing this List Comprehension for calculating the value for result?

    nothing = [0,0,0,0,0,0]
    box = [1,2]
    boxes = [box,box,box]
    page = [boxes,boxes]
    pages = [page, page]
    npr = [nothing, pages]

**result =  [box for box in npr.pages[i].boxes] where i is the counter variable**

P.S. This is pseudocode.

shantanu rahut
  • 189
  • 1
  • 4
  • 15
  • 1
    it is not really clear, but you do not need any loop, only: `npr[1][0]` – mozway Oct 05 '21 at 07:43
  • 3
    You've reused/overwritten the `boxes` variable in your `for` loop so it's not clear what you are doing or intend – Iain Shelvington Oct 05 '21 at 07:43
  • This will always return the content of `boxes` in to another array. You could copy the `boxes` in to `result` as well. – MSH Oct 05 '21 at 08:08
  • Hi @mozway I have edited the question. I hope it is clear now. – shantanu rahut Oct 05 '21 at 08:09
  • @shantanurahut not really, I suggest you take time to think about the question, write it in the most understandable way, describe the logic, include examples of input and output, and ask a new question – mozway Oct 05 '21 at 08:11
  • Hi @MSH I have edited the question to be more clear. Can you please help me with this ? Thanks a lot. – shantanu rahut Oct 05 '21 at 08:11

1 Answers1

3

Like this:

result = [i for boxes in npr[1] for i in boxes]
print(result)

Output:

[1, 2, 3, 4, 5, 6]

In this case why not just use:

result = npr[1][0]

Or:

result = sum(npr[1], [])
U13-Forward
  • 69,221
  • 14
  • 89
  • 114
  • Hi @U12-Forward. I have edited the question. I hope it is more clear now. Can you please help me with this? – shantanu rahut Oct 05 '21 at 08:08
  • 1
    @shantanurahut Please ask a new question instead, I already solved your original problem, ask a new question please, and to close this question, please accept this. – U13-Forward Oct 05 '21 at 08:15