M
M
Moro Zota2018-04-25 15:25:53
Python
Moro Zota, 2018-04-25 15:25:53

How does .map work in python?

Started learning python. Let's say there is a function that takes a list of words. How to go through all the elements of this list and for example add a '!' ?
Here in js it is possible through .map , but in python? Explain in more detail please

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2018-04-25
@morozota

And in Python, you can use map:

words = ['hello', 'world']
with_exclamation = list(map(lambda s: s + '!', words))

But it's more idiomatic to use a list comprehension:
with_exclamation = [s + '!' for s in words]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question