0

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?

Byeongguk Gong
  • 113
  • 1
  • 9
dublejin
  • 37
  • 1
  • 8
  • 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
  • 1
    callback 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 Answers1

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