Python: Functions used on each element of list x vs Functions that take list x as arg -


is there reason choose 1 style on other? example:

def add_10(x):     return x + 10  results = [add_10(i) in range(5)] 

vs

def add_10(list_):    return [e + 10 e in list_]  results = add_10(range(5)) 

this basic example, illustrates idea. have been using former because writing functions simple inputs , outputs (not lists complicated), don't know if there real pro's or con's other personal style. see more of latter in documentation , other people's code, can't find resources address specific idea.

thanks

the first version more general, can used values in kind of data structure (or not in structure, single variable). instance, if have dictionary, use create new dictionary elements increased 10. or can write:

input_plus_10 = add_10(int(input("please enter number"))) 

the second version more specific, can used lists. if that's thing need, it's fine (but should name list_add_10).


Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -