I want to create a list of squares of even numbers by creating a list, applying the callback functions to each element of the list and puts the result in a list. But how do I implement callback in the first place?
Asked
Active
Viewed 5,895 times
0
-
A callback is a function (technically, any callable, including methods, classes and instances of callable classes, not just functions). Aside from that, you'll need to be more specific. What are you using that needs one? – ShadowRanger Dec 13 '17 at 04:11
-
1callback mostly means function name without `()`. You can use it as argument in other function and it will execute it later using `()`. So every function or method can be callback. For example in `tkinter` you can assign function name to button and button will execute this function later, when you click this button. So assigned function is called "callback". – furas Dec 13 '17 at 04:21
1 Answers
1
In this case I think you are looking for the map
builtin. It takes a function and a list and then applies that function on the list storing each result as it goes. For example:
def square(x):
return x * x
list(map(square, [1, 2, 3, 4]))
>>> [1, 4, 9, 16]
Note that we need to cast the result of map
back to a list
since it returns a map object.

mattjegan
- 2,724
- 1
- 26
- 37
-
1
-
1@RohitMenon: `map`'s first argument is a callback function which it applies to each element of the input, with the output being the result of executing said callback. – ShadowRanger Dec 13 '17 at 04:13
-
-
@KlausD. [Callbacks](https://en.wikipedia.org/wiki/Callback_(computer_programming)) – s.ouchene Jan 08 '19 at 14:16