Is there a value in using map() vs for?

前端 未结 8 2858
误落风尘
误落风尘 2021-02-20 17:03

Does map() iterate through the list like \"for\" would? Is there a value in using map vs for?

If so, right now my code looks like this:

for item in item         


        
8条回答
  •  抹茶落季
    2021-02-20 17:26

    Use an explicit for-loop when you don't need a list of results back (eg. functions with side-effects).

    Use a list comprehension when you do need a list of results back (eg. functions that return a value based directly on the input).

    Use map() when you're trying to convince Lisp users that Python is worth using. ;)

提交回复
热议问题