1

I'm new to python and I need help to re-write that for loop function with indentations below

y = [g(x,y) for x in xs for y in xs if f(x,y)]

full code is here

g = lambda x,y:(x-y) 
f = lambda x,y: x>y 
xs = [1,2,3,4] 
y = [g(x,y) for x in xs for y in xs if f(x,y)]
8
  • Why do you need expand that list comprehension? Commented Jun 8, 2016 at 3:39
  • That code is an example, I need a simpler form of for loop to understand so I can visualize. Commented Jun 8, 2016 at 3:40
  • Do you understand the lambda's? Commented Jun 8, 2016 at 3:52
  • Not really, I'm trying to understand it. Any suggestions? Commented Jun 8, 2016 at 3:53
  • The first can be defined as a function that takes two arguments and returns the difference between the first and second argument. Commented Jun 8, 2016 at 3:56

1 Answer 1

1
y = []
for a in xs:
    for b in xs:
        if f(a, b):
            y.append(g(a, b))
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, It is easier for me to understand the function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.