JaneDoe
JaneDoe

Reputation: 171

How pipe the output of for loop into another variable

I have the following code:

for {for loop assertion }:
    if (a) > 0 and b > 0:
        print('same')
    elif (a) < 0 and b < 0:
        print('same')
    else:
        print('different')

This produces a list like this:

different
different
same
different
same
different
same
different
different
same
different
different
different
different
different
same

What I want to do is save this to another variable. Could you please advise how I can accomplish that?

Upvotes: 0

Views: 151

Answers (1)

The shape
The shape

Reputation: 359

Try this, it should work, if not tell me why:

lst = []
for {for loop assertion }:
        if (a) > 0 and b > 0:
            lst += ["same"]
        if (a) < 0 and b < 0:
            lst += ["same"]
        else:
            lst += ["different"]

or you could do:

    lst = []
    for {for loop assertion }:
            if (a) > 0 and b > 0:
                lst.append("same")
            if (a) < 0 and b < 0:
                lst.append("same")
            else:
                lst.append("different")

Upvotes: 3

Related Questions