{"id":"strings-and-lists-slices","slug":"slices","title":"Slices with a step","text":"You know slices from strings: `text[a:b]` is the part from index `a` up to, but not including, index `b`. Lists can be sliced in just the same way, and a slice of a list is a new list:\n\n```python\nword = \"keyboard\"\nprint(word[3:6])\nnumbers = [10, 20, 30, 40, 50, 60]\nprint(numbers[1:4])\nprint(numbers[:2])\nprint(numbers[-2:])\nprint(numbers)\n```\n\n```output\nboa\n[20, 30, 40]\n[10, 20]\n[50, 60]\n[10, 20, 30, 40, 50, 60]\n```\n\nLeaving out the first index starts from the beginning, and leaving out the second goes to the end, so `numbers[-2:]` is the last two items. The original list doesn't change: the slice is a copy of part of it.\n\nA slice never stops the program with an `IndexError`. If it reaches past the end, it simply has fewer items: `numbers[4:100]` is `[50, 60]`.\n\n### The step\n\nLike `range`, a slice can have a third number, the **step**. `[a:b:2]` takes every second item:\n\n```python\nletters = \"abcdefgh\"\nprint(letters[0:8:2])\nprint(letters[1::2])\nprint(letters[::3])\n```\n\n```output\naceg\nbdfh\nadg\n```\n\n`letters[1::2]` starts from index 1 and goes to the end, taking every second character. `letters[::3]` leaves out both ends and takes every third character of the whole string.\n\n### Going backwards\n\nWith a negative step, the slice goes from right to left. The start is then the greater index. Leaving out both ends with the step -1 gives the whole string or list in reverse:\n\n```python\nnumbers = [10, 20, 30, 40, 50, 60]\nprint(numbers[4:1:-1])\nprint(numbers[::-1])\nword = input(\"Word: \")\nprint(word[::-1])\n```\n\n```input\ndrawer\n```\n\n```output\n[50, 40, 30]\n[60, 50, 40, 30, 20, 10]\nWord: drawer\nreward\n```\n\n`[::-1]` is the shortest way to reverse a string in Python.\n\n### Joining lists\n\nLike strings, two lists can be joined with `+`. The result is a new list with the items of both, in order:\n\n```python\nfirst = [1, 2, 3]\nsecond = [7, 8]\nprint(first + second)\nprint(first[::-1] + second)\n```\n\n```output\n[1, 2, 3, 7, 8]\n[3, 2, 1, 7, 8]\n```\n","task":"Fill in the blank with a slice, so the program prints the word backwards and tells whether it's a **palindrome**, a word that reads the same both ways:\n\n```\nWord: level\nlevel\nA palindrome!\n```\n","kind":"fill","seed":"word = input(\"Word: \")\nbackwards = word[[[::-1]]]\nprint(backwards)\nif word == backwards:\n    print(\"A palindrome!\")\n","solution":"word = input(\"Word: \")\nbackwards = word[::-1]\nprint(backwards)\nif word == backwards:\n    print(\"A palindrome!\")\n","hints":["A slice with a negative step goes backwards.","Leave out both ends and use the step -1: `[::-1]`."],"tests":[{"name":"Test 1","stdin":"level\n","expect":"Word: level\nlevel\nA palindrome!","hidden":false},{"name":"Test 2","stdin":"python\n","expect":"Word: python\nnohtyp","hidden":true},{"name":"Test 3","stdin":"racecar\n","expect":"Word: racecar\nracecar\nA palindrome!","hidden":true}],"require":[],"forbid":[],"more":[{"task":"A simple way to shuffle a deck of cards is to take every second card from the top into one pile, and put the rest after them. Write the function `shuffle(cards: list) -> list`, which returns a new list with the cards at indexes 0, 2, 4, ... first and then the cards at indexes 1, 3, 5, ... For example, `shuffle([1, 2, 3, 4, 5])` returns `[1, 3, 5, 2, 4]`.\n","kind":"code","seed":"def shuffle(cards: list) -> list:\n    pass\n\n\nif __name__ == \"__main__\":\n    print(shuffle([1, 2, 3, 4, 5]))\n","solution":"def shuffle(cards: list) -> list:\n    return cards[::2] + cards[1::2]\n\n\nif __name__ == \"__main__\":\n    print(shuffle([1, 2, 3, 4, 5]))\n","hints":["A slice with step 2 takes every second item. Starting from index 0 gives the first pile.","Starting from index 1 with the same step gives the rest: `cards[1::2]`.","Join the two slices with `+` and return the result."],"tests":[{"name":"shuffle([1, 2, 3, 4, 5])","stdin":"","call":"shuffle([1, 2, 3, 4, 5])\n","expect":"[1, 3, 5, 2, 4]","hidden":false,"returns":true},{"name":"shuffle([\"A\", \"K\", \"Q\", \"J\"])","stdin":"","call":"shuffle([\"A\", \"K\", \"Q\", \"J\"])\n","expect":"['A', 'Q', 'K', 'J']","hidden":true,"returns":true},{"name":"shuffle([7])","stdin":"","call":"shuffle([7])\n","expect":"[7]","hidden":true,"returns":true}],"require":[],"forbid":[]},{"task":"What does this program print? Type each line it prints.\n","kind":"predict","seed":"text = \"python\"\nprint(text[1:4])\nprint(text[::2])\nprint(text[-3:])\nprint(text[4:0:-1])\nnumbers = [5, 6, 7, 8]\nprint(numbers[::-1][:2])\n","solution":"text = \"python\"\nprint(text[1:4])\nprint(text[::2])\nprint(text[-3:])\nprint(text[4:0:-1])\nnumbers = [5, 6, 7, 8]\nprint(numbers[::-1][:2])\n","hints":["`text[1:4]` takes indexes 1, 2 and 3. `text[::2]` takes every second character from the start.","`text[4:0:-1]` goes backwards from index 4 and stops before index 0.","`numbers[::-1]` is the reversed list, and `[:2]` takes its first two items."],"tests":[{"name":"Output","stdin":"","expect":"yth\npto\nhon\nohty\n[8, 7]","hidden":false}],"require":[],"forbid":[],"lines":["yth","pto","hon","ohty","[8, 7]"]}]}