Search code examples
pythonternary

How can I expand this list comprehension into a multi-line for loop?


I am trying to un condense:

samp_neighs = [_set(_sample(to_neigh, 
                            num_sample,
                            )) if len(to_neigh) >= num_sample else to_neigh for to_neigh in to_neighs]

into multiple lines. Can someone help? Thank you in advance!


Solution

  • If I wrote out your code in a few lines rather than a single line it would look something like this:

    result = []
    
    for to_neigh in to_neighs:
        if len(to_neigh) >= num_sample:
            result.append(_set(_sample(to_neigh, num_sample)))
        else:
            result.append(to_neigh)