Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (18.4k points)

What's the one-liner for the below code?

for k,v in d2.items():

    if d1.get(k,0) < v:

        return False

return True

I tried this but it's invalid syntax.

return False if d1.get(k,0)<v for k,v in d2.items() else True

Why?

1 Answer

0 votes
by (36.8k points)

Use any or all:

return not any(d1.get(k, 0) < v for k, v in d2.items())

or

return all(d1.get(k, 0) >= v for k, v in d2.items())

Do check out Data Science with Python course which helps you understand from scratch.

Browse Categories

...