1

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.

3 Answers 3

2

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']
Sign up to request clarification or add additional context in comments.

Comments

1

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]

Comments

0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.