Search code examples
pythonpython-3.xlist-comprehensionpython-all-function

Evaluating 2 list using all function throwing error in python


I am using the all function to compare list items to see if they are less than or equall to 10 of each other. I am testing to see if the values in l2 is 10 lower than value in l1. But I am getting a syntax error.

l1 = [10, 20, 30, 40, 50]
l2 = [50, 75, 30, 20, 40]

all([result for x,y in l1,l2 if x - y<=10 ])


SyntaxError: invalid syntax
all([result for x,y in l1,l2 if x - y<=10 ])

Solution

  • Figured out what I wanted.

    result = all(map(lambda x, y: (x-y) >=10, l1, l2))
    print(f"result:{result}")
    
    res = all([l1[a]-l2[a]>=10 for a in range(len(l1))])
    print(f"result:{res}")