normal way:
for x in myList:
myFunc(x)
you must use a variable x
use
map(myFunc,myList)
and in fact you must use this to make above work
list(map(myFunc,myList))
that would build a list,i don't need to build a list
maybe some one would suggest me doing this
def func(l):
for x in l:
....
that is another topic
is there something like this?
every(func,myList)
The 'normal way' is definitely the best way, although itertools
does offer the consume recipe for whatever reason you might need it:
import collections
from itertools import islice
def consume(iterator, n):
"Advance the iterator n-steps ahead. If n is none, consume entirely."
# Use functions that consume iterators at C speed.
if n is None:
# feed the entire iterator into a zero-length deque
collections.deque(iterator, maxlen=0)
else:
# advance to the empty slice starting at position n
next(islice(iterator, n, n), None)
This could be used like:
consume(imap(func, my_list), None) # On python 3 use map
This function performs the fastest as it avoids python for loop overhead by using functions which run on the C side.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With