N
N
NOblo2020-08-03 00:44:08
Python
NOblo, 2020-08-03 00:44:08

Why is no one using map?

Why do this:

a = [5, 10, 20, 40, 80]
j = [i**2/5 for i in a]

if you can like this: Okay, I myself realized that with for the option is shorter, but what remains better? Which code will be considered more optimized with option 1 or option 2? Or does it matter?
j = list(map(lambda x: x**2/5, a))

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexander, 2020-08-03
@sanya84

Maybe because not everyone likes "buns"?

S
SagePtr, 2020-08-03
@SagePtr

At a minimum, the variant with for is clearer and more readable.
From the point of view of performance, in both cases O (n), the map function does not do any "magic", the same loop runs inside.

R
Roman Kitaev, 2020-08-03
@deliro

The first is clearer and faster

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question