Various list comprehensions

Question Q4.3.3

Given the lists

>>> a = ['A', 'B', 'C', 'D', 'E', 'F', 'G']
>>> b = [4, 2, 6, 1, 5, 0, 3]

Predict and explain the output of the following statements:

(a) [a[x] for x in b]

(b) [a[x] for x in sorted(b)]

(c) [a[b[x]] for x in b]

(d) [x for (y,x) in sorted(zip(b,a))]


Solution