Home > Blockchain >  How can I directly obtain the item from this list comprehension?
How can I directly obtain the item from this list comprehension?

Time:11-23

Here's part of the code I'm working on in Python:

def ee(mid_circ = False):
    initial_layout = [[1,0] if mid_circ == True else [1,2,3,0]]
    return initial_layout

However, the output is either [[1,0]] or [[1,2,3,0]]. Is there a way I can directly obtain [1,0] or [1,2,3,0] from the code?

CodePudding user response:

That's not a list comprehension. You are simply constructing a list consisting of a single item (which also happens to be a list).

You should simply remove the outer brackets, i.e.

initial_layout = [1,0] if mid_circ else [1,2,3,0]

Note that I've also dropped the redundant == True check.

  • Related