Reputation: 103
I'm currently stuck in this right now.
I have a code right here:
words = ["Hello","how","are","you"]
arrlen = len(words)
val1, val2, val3, val4 = words
What I want to do is add ".mp4"
in each val1
, val2
, val3
and val4
. Is there any way to achieve this? I have tried val1 + ".mp4"
, but it does not work.
Upvotes: 1
Views: 42
Reputation: 51185
Using a list comprehension:
>>> words = ["Hello","how","are","you"]
>>> words = [x + ".mp4" for x in words]
>>> words
['Hello.mp4', 'how.mp4', 'are.mp4', 'you.mp4']
Upvotes: 2
Reputation: 5968
You can use the built-in map()
function.
Directly from the docs:
Returns an iterator that applies function to every item of iterable, yielding the results.
Here the function is a lambda expression instead and maps each element from words
with + '.mp4'
, which is exactly what you want:
words = ['Hello', 'how', 'are', 'you']
words = list(map(lambda s: s + '.mp4', words))
print(words)
Upvotes: 0
Reputation: 71471
Mutate each item along with the unpacking:
words = ["Hello","how","are","you"]
arrlen = len(words)
val1, val2, val3, val4 = ["{}.mp4".format(word) for word in words]
Upvotes: 1