{"id":"for-loops-building-lists","slug":"building-lists","title":"Ranges, lists and new lists","text":"`range` doesn't make a list. A range hands out its numbers one at a time, whenever a loop asks for the next one, so it never needs to hold them all. That's why printing a range shows just the call that made it, not its numbers:\n\n```python\nhours = range(8, 13)\nprint(hours)\nprint(list(hours))\nprint(list(range(10, 0, -3)))\n```\n\n```output\nrange(8, 13)\n[8, 9, 10, 11, 12]\n[10, 7, 4, 1]\n```\n\nThe function **`list`** turns a range into a real list with all its numbers. A `for` loop doesn't need that: it goes through a range just as well as through a list.\n\n### Building a new list\n\nA common job is to make a new list from an old one. Start with an empty list and `append` to it in the loop. The new list can keep only some of the items:\n\n```python\ndef passed(scores: list) -> list:\n    result = []\n    for score in scores:\n        if score >= 50:\n            result.append(score)\n    return result\n\n\nscores = [72, 35, 50, 90, 48]\nprint(passed(scores))\nprint(scores)\n```\n\n```output\n[72, 50, 90]\n[72, 35, 50, 90, 48]\n```\n\nor change each item on the way:\n\n```python\ndef half_price(prices: list) -> list:\n    result = []\n    for price in prices:\n        result.append(price / 2)\n    return result\n\n\nprint(half_price([10, 4, 30]))\n```\n\n```output\n[5.0, 2.0, 15.0]\n```\n\nEither way, the original list stays as it was, and the function returns the new one.\n\n### When you need the index\n\nSometimes the item alone isn't enough: you need its index too, for example to compare it with the item next to it, or with the item at the same index in another list. Then go through the indexes with `range(len(...))`:\n\n```python\nhome = [2, 0, 3]\naway = [1, 0, 4]\nfor i in range(len(home)):\n    print(f\"Game {i + 1}: {home[i]}-{away[i]}\")\n```\n\n```output\nGame 1: 2-1\nGame 2: 0-0\nGame 3: 3-4\n```\n\n`range(len(home))` gives the indexes 0, 1 and 2, and `home[i]` and `away[i]` are the items at the same index in the two lists.\n","task":"Write the function `multiples(number: int, count: int) -> list`, which returns a list of the first `count` multiples of `number`. For example, `multiples(3, 4)` returns `[3, 6, 9, 12]`.\n","kind":"code","seed":"def multiples(number: int, count: int) -> list:\n    pass\n\n\nif __name__ == \"__main__\":\n    print(multiples(3, 4))\n","solution":"def multiples(number: int, count: int) -> list:\n    result = []\n    for i in range(1, count + 1):\n        result.append(number * i)\n    return result\n\n\nif __name__ == \"__main__\":\n    print(multiples(3, 4))\n","hints":["Start with an empty list `result`, and return it after a loop that adds one item in each round.","The items are `number * 1`, `number * 2` and so on, up to `number * count`. Go through those multipliers with `for i in range(1, count + 1):`.","In the loop, `result.append(number * i)`."],"tests":[{"name":"multiples(3, 4)","stdin":"","call":"multiples(3, 4)\n","expect":"[3, 6, 9, 12]","hidden":false,"returns":true},{"name":"multiples(5, 1)","stdin":"","call":"multiples(5, 1)\n","expect":"[5]","hidden":true,"returns":true},{"name":"multiples(7, 6)","stdin":"","call":"multiples(7, 6)\n","expect":"[7, 14, 21, 28, 35, 42]","hidden":true,"returns":true}],"require":[],"forbid":[],"more":[{"task":"Write the function `long_words(words: list, min_length: int) -> list`, which returns a new list of the words that have at least `min_length` characters, in their original order. For example, `long_words([\"sun\", \"planet\", \"moon\", \"galaxy\"], 5)` returns `['planet', 'galaxy']`.\n","kind":"code","seed":"def long_words(words: list, min_length: int) -> list:\n    pass\n\n\nif __name__ == \"__main__\":\n    print(long_words([\"sun\", \"planet\", \"moon\", \"galaxy\"], 5))\n","solution":"def long_words(words: list, min_length: int) -> list:\n    result = []\n    for word in words:\n        if len(word) >= min_length:\n            result.append(word)\n    return result\n\n\nif __name__ == \"__main__\":\n    print(long_words([\"sun\", \"planet\", \"moon\", \"galaxy\"], 5))\n","hints":["Start with an empty list `result` and go through `words` with a `for` loop.","Append a word when `len(word) >= min_length`.","Return `result` after the loop. A list with no long words gives the empty list `[]`."],"tests":[{"name":"long_words([\"sun\", \"planet\", \"moon\", \"galaxy\"], 5)","stdin":"","call":"long_words([\"sun\", \"planet\", \"moon\", \"galaxy\"], 5)\n","expect":"['planet', 'galaxy']","hidden":false,"returns":true},{"name":"long_words([\"a\", \"bb\", \"ccc\"], 2)","stdin":"","call":"long_words([\"a\", \"bb\", \"ccc\"], 2)\n","expect":"['bb', 'ccc']","hidden":true,"returns":true},{"name":"long_words([\"tiny\", \"word\"], 9)","stdin":"","call":"long_words([\"tiny\", \"word\"], 9)\n","expect":"[]","hidden":true,"returns":true}],"require":[],"forbid":[]},{"task":"Write the function `changes(values: list) -> list`, which returns a list of how much each value changed from the one before it. The new list has one item less than the original. For example, `changes([3, 5, 4, 10])` returns `[2, -1, 6]`: 5 - 3 is 2, 4 - 5 is -1 and 10 - 4 is 6.\n","kind":"code","seed":"def changes(values: list) -> list:\n    pass\n\n\nif __name__ == \"__main__\":\n    print(changes([3, 5, 4, 10]))\n","solution":"def changes(values: list) -> list:\n    result = []\n    for i in range(1, len(values)):\n        result.append(values[i] - values[i - 1])\n    return result\n\n\nif __name__ == \"__main__\":\n    print(changes([3, 5, 4, 10]))\n","hints":["Each change needs two items next to each other, so go through the indexes instead of the items.","Start from index 1, since the first item has no item before it: `for i in range(1, len(values)):`.","The change at index `i` is `values[i] - values[i - 1]`. Append it to a new list and return the list after the loop."],"tests":[{"name":"changes([3, 5, 4, 10])","stdin":"","call":"changes([3, 5, 4, 10])\n","expect":"[2, -1, 6]","hidden":false,"returns":true},{"name":"changes([7])","stdin":"","call":"changes([7])\n","expect":"[]","hidden":true,"returns":true},{"name":"changes([10, 8, 8, 1, 20])","stdin":"","call":"changes([10, 8, 8, 1, 20])\n","expect":"[-2, 0, -7, 19]","hidden":true,"returns":true}],"require":[],"forbid":[]}]}