Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Condition statement without loops

Tags:

python

I'm writing a small function to check if all elements in a list are less than or equal to a limit. This is just for practice, and should be done without using any loops.

def small_enough(a, limit): 
    return all(x <= limit for x in a)
small_enough([66, 101], 200)

Been on this for a while but I can not find any substituting code to replace the for loop.This code works perfectly fine as it is - however, I am trying to get the results without using loops. Trying to write something a bit more 'pythonic'.

like image 825
pragmatic learner Avatar asked Aug 27 '19 14:08

pragmatic learner


People also ask

How do you express conditional logic without using conditional statements?

To help accomplish this, we look at a way to express conditional logic without using conditional statements. It is possible and sometimes preferable to express conditional logic without using conditional statements, by embedding the selection logic in an appropriate data structure.

Can you implement conditional logic without conditional statements in Swift?

It is quite natural to associate conditional logic with conditional statements, the most common being the if and if-else statements. In reality though, conditional logic can be implemented not only using the rich set of conditional statements provided by Swift but also without using any conditional statements at all.

What are conditional statements in Excel VBA?

Conditional Statements in Excel VBA are very useful in programming, this will give you to perform comparisons to decide or loop through certain number of iterations based on a criteria. In this tutorial we will learn the conditional statements with examples. » If … Else Statement

What is the use of for loop in C?

You can use loops to execute statements certain number of time or until it satisfies some condtion. For loop is useful to execute statements certain number of time. The following example show you the message box 5 times with integers


Video Answer


1 Answers

If using numpy is ok, you can do

import numpy as np

x = np.asarray([66,101])
print(all(x <= 200))
like image 154
user2653663 Avatar answered Oct 13 '22 20:10

user2653663