D
D
Daniil Shevkunov2021-08-31 15:03:30
Python
Daniil Shevkunov, 2021-08-31 15:03:30

How to generate an array of functions?

How to simplify code like this:

mas = []
for i in range(10):
    def f(i):
        mas.append(lambda: print(i))
    f(i)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Drill, 2021-08-31
@danila763

mas = [(lambda i: lambda : print(i))(i) for i in range(10)]

L
Larisa .•º, 2021-08-31
@barolina

not quite, apparently, the example conveys the essence of what needs to be done,
maybe like this

mas = []
for i in range(10):
        mas.append(i)

or
mas = []
def f(i):
     mas.append(i)     
for i in range(10):
      f(i)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question