JIT — Intro to Data Science · Python Lab · Problem 17 of 30

The same job in one line

A loop, an empty list and an append — collapsed into a single line that reads back to front.

Five lines and one line, doing the same thing

Building a new list out of an old one is so common that Python has a shorthand for it. You already know the long way, from problem 15: make an empty list, loop, append to it. The short way says the same thing on one line, and once you can read it you will see it everywhere in other people's code.

New word — list comprehension
percents = [] for mark in marks: percents.append(mark * 2) percents = [mark * 2 for mark in marks]
Both build the same list. The second is read in an odd order: what to collect comes first, then where to get it from.
Out loud: collect mark times two, for each mark in marks. The square brackets are what make the result a list.
The expression comes first, and that is the part everyone gets wrong Every other construction you have met reads top to bottom, left to right. This one puts the answer at the front and the source at the back. Written the other way round it is not merely unusual — Python cannot read it at all, and the message it gives points at the closing bracket rather than at the part that is out of place.
Adding a filter [mark for mark in marks if mark >= 35]
The if goes at the end and decides which items get collected. Items that fail it are skipped, so the new list comes out shorter than the old one. This is a filter, not a decision about what to collect — there is no else, because an item either goes in or it does not.
Check one against the other Two lists can be compared directly: percents == percents2 is True only if they hold the same values in the same order. Whenever you replace working code with a shorter version, keep the old one alongside for a run or two and compare. It costs nothing and it is the only proof you have that the rewrite did not change the answer.

What you are building

Marks out of 50: [39, 33, 40, 46, 27] By loop: [78, 66, 80, 92, 54] By comprehension: [78, 66, 80, 92, 54] Same answer? True Scored 35 or more: [39, 40, 46] How many: 3
Coming later A comprehension walks a list one item at a time, however short the line looks. NumPy does the same job to a whole array at once, without walking anything — marks * 2 with no loop at all, in problem 18.

Build the programme

Your plan

The steps still to place

The whole programme is laid out below. The loop version is written out in full and left alone — it is your reference. Five pieces are missing from everything else, and one of the printed lines exists purely to tell you whether the short version really matches the long one.

Leave nothing on "choose…". A wrong pick does not always cause an error — sometimes it just prints something you did not expect, which is the harder kind of mistake to spot.

Run and read

Here is the whole programme. The marks are out of 50 and yours to change. Keep an eye on the "Same answer?" line — that is the check doing its job.

Your programme

Output

Nothing has run yet.