Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is there some way execute every element simply?

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)
like image 703
Max Avatar asked Jan 16 '23 12:01

Max


1 Answers

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.

like image 67
jamylak Avatar answered Jan 25 '23 02:01

jamylak