Skip to content

Question 45

Given a list L of strings, complete function double_reverse() with valid Python expressions/statements such that it returns a list containing the reverse of all strings in L, where they are also in reverse order from L.

def double_reverse(L):
    return _____1_____

Here is a sample run:

>>> print(double_reverse(['ti', 'od', 'uoy', 'nac']))
['can', 'you', 'do', 'it']

Note that your code needs to be syntactically correct to gain marks. You cannot use any semi-colon(;) and we will deduct marks if your answer is too long.

Model Solution

Hint 1

You will need list comprehension for this question. What you can try to do is work out the solution without it first using a regular loop structure before converting it into its list comprehension equivalent.

Hint 2 (read Hint 1 first!)

Compress the highlighted lines 2-5 into one line.

1
2
3
4
5
6
7
def double_reverse(L):
    output = []
    # append reverse of each word `c` into `output`
    for c in L:
        output.append(c[::-1])
    # return `output` with the position of its elements in reverse
    return output[::-1]

Use [::-1] to help with reversing a string value.

Model Solution
def double_reverse(L):
    return [c[::-1] for c in L][::-1]

Blank 1: [c[::-1] for c in L][::-1]

Other acceptable answer(s):

  • list(map((lambda i: i*n), L))