What is the python equivalent of JavaScript's Array.prototype.some / every?

Question:

Does python have any equivalent to JavaScript’s Array.prototype.some / every?

Trivial JavaScript example:

var arr = [ "a", "b", "c" ];
arr.some(function (element, index) {
    console.log("index: " + index + ", element: " + element)
    if(element === "b"){
        return true;
    }
});

Will output:

index: 0, element: a
index: 1, element: b

The below python seems to be functionally equivalent, but I do not know if there is a more “pythonic” approach.

arr = [ "a", "b", "c" ]
for index, element in enumerate(arr):
    print("index: %i, element: %s" % (index, element))
    if element == "b":
        break
Asked By: shaund

||

Answers:

Python has all(iterable) and any(iterable). So if you make a generator or an iterator that does what you want, you can test it with those functions. For example:

some_is_b = any(x == 'b' for x in ary)
all_are_b = all(x == 'b' for x in ary)

They are actually defined in the documentation by their code equivalents. Does this look familiar?

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
Answered By: Amadan

No. NumPy arrays have, but standard python lists don’t. Even so, the numpy array implementations are not what you’d expect: they don’t take a predicate, but evaluate every element by converting them to boolean.

Edit: any and all exist as functions (not as methods), but they don’t apply predicates, but consider booleanized values as numpy methods.

In Python, some could be:

def some(list_, pred):
    return bool([i for i in list_ if pred(i)])

#or a more efficient approach, which doesn't build a new list
def some(list_, pred):
    return any(pred(i) for i in list_) #booleanize the values, and pass them to any

You could implement every:

def every(list_, pred):
    return all(pred(i) for i in list_)

Edit: dumb sample:

every(['a', 'b', 'c'], lambda e: e == 'b')
some(['a', 'b', 'c'], lambda e: e == 'b')

Try them by urself

Answered By: Luis Masuelli
numbers = [1, 2, 3, 4, 5, 6, 7, 8]
def callback( n ):
    return (n % 2 == 0)

# 1) Demonstrates how list() works with an iterable
isEvenList = list( callback(n) for n in numbers)
print(isEvenList)

# 2) Demonstrates how any() works with an iterable
anyEvenNumbers = any( callback(n) for n in numbers)
print(anyEvenNumbers)

# 3) Demonstrates how all() works with an iterable
allEvenNumbers = all( callback(n) for n in numbers)
print(allEvenNumbers)
  1. list() takes any iterable, returns another list:
isEvenList = list(callback(n) for n in numbers)
print(isEvenList)
#[False, True, False, True, False, True, False, True]
  1. any(): needs just one True in a list of booleans, returns a boolean:
anyEvenNumbers = any( callback(n) for n in numbers)
print(anyEvenNumbers)
#True
  1. all(): needs all True values in a list of booleans, returns a boolean:
allEvenNumbers = all( callback(n) for n in numbers)
print(allEvenNumbers)
#False
Answered By: Jesse Willard
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.