Skip to main content

Retrieving Collection Parts

Sometimes you do not need a whole list. You need a part of it: a range of elements, the first or last few, pieces of equal size, or a moving window of neighbours.

Python has one syntax for most of this: the slice. words[low:high] gives the elements from index low up to, but not including, index high. Leave out low to start at the beginning, or leave out high to go to the end. A negative index counts back from the end, and a third number sets a step.

  • s[low:high] — from index low up to, but not including, high
  • s[:n] — the first n elements
  • s[n:] — everything from index n to the end
  • s[-n:] — the last n elements
  • s[::2] — every second element

A slice always builds a new list and copies the chosen elements into it. Changing the copy does not change the original, and changing the original does not change the copy. In some other languages the same operation gives a view that shares memory with the original — in Python it never does.

For the jobs a slice cannot do on its own, this lesson uses functions from the itertools module and a small helper function or two.

Most examples on this page use the same data: the words one two three four five six.


Slice

Slicing means taking the elements at some chosen positions.

For a run of neighbouring positions, use a slice: words[1:4] gives the elements at indexes 1, 2 and 3. Remember that the end index is excluded: to get the elements at indexes 1 to 3 you write 1:4.

A slice can even skip elements: the third number is the step, so words[::2] takes every second element. For positions you list yourself, in your own order, use a list comprehension over a list of indexes.

Slice a range

words[1:4] starts at index 1 and stops before index 4. The result has 3 elements: ['two', 'three', 'four']. It is a new list, so a change to it would not touch words.

Prompt
Make a list of the words one, two, three, four, five, six. Use a slice to take the elements from index 1 up to, but not including, index 4. Print the result.
Output: complete Python program
Slice a range

Slice with a step

The slice words[::2] has an empty start and end, so it covers the whole list, and the step 2 takes every second element: indexes 0, 2 and 4. No loop is needed — the step is part of the slice syntax.

Prompt
Make a list of the words one, two, three, four, five, six. Use a slice with a step of 2 to take every second element. Print the result.
Output: complete Python program
Slice with a step

Slice by a list of indices

The positions 3, 5 and 0 are stored in a list of numbers. The comprehension picks the word at each position, so the result comes out in the order of the indexes.

Prompt
Make a list of the words one, two, three, four, five, six and a list of indexes 3, 5, 0. Use a list comprehension over the indexes to collect the words at those positions, in that order. Print the result.
Output: complete Python program
Slice by a list of indices

Take and Drop

Take means keep a number of elements from one end. Drop means throw away a number of elements from one end and keep the rest.

Both are slices in Python. words[:3] takes the first 3 elements and words[-3:] takes the last 3. words[1:] drops the first element and words[:-5] drops the last 5.

There are also conditional versions. These do not count elements: they walk from one end and stop at the first element that fails a condition. Python has them built in, in the itertools module: takewhile() keeps elements from the start while a condition holds, and dropwhile() skips elements while it holds and keeps the rest. Both return a lazy iterator, so wrap the call in list() to see the elements. For the same jobs from the end of the list there is no built-in: run the function over reversed(words) and turn the result back around with [::-1].

Take the first or last n elements

words[:3] keeps the first 3 words. words[-3:] keeps the last 3, because a negative index counts back from the end.

Prompt
Make a list of the words one, two, three, four, five, six. Use slices to get the first 3 elements and the last 3 elements. Print both results on separate lines.
Output: complete Python program
Take the first or last n elements

Drop the first or last n elements

words[1:] skips the first word. words[:-5] skips the last 5 words, so only one is left.

Prompt
Make a list of the words one, two, three, four, five, six. Use slices to get everything except the first element, and everything except the last 5 elements. Print both results on separate lines.
Output: complete Python program
Drop the first or last n elements

Take while a condition holds

takewhile() keeps words from the start until it meets a word that starts with f. The condition is a lambda, a small nameless function, and startswith() checks the start of a string. For the end of the list, takewhile() runs over reversed(words) — it keeps words until it meets three — and [::-1] turns the result back around.

Prompt
Take the words one, two, three, four, five, six. Use itertools.takewhile to keep words from the start while the word does not start with f. Then keep words from the end while the word is not three, by running takewhile over the reversed list and turning the result back around. Print both results.
Output: complete Python program
Take while a condition holds

Drop while a condition holds

dropwhile() skips the 3-letter words at the start and keeps everything from three on. The same trick with reversed() skips words from the end while they contain the letter i. The in operator checks whether a string holds a piece of text.

Prompt
Take the words one, two, three, four, five, six. Use itertools.dropwhile to skip words from the start while the word has 3 letters, keeping the rest. Then skip words from the end while the word contains the letter i, by running dropwhile over the reversed list and turning the result back around. Print both results.
Output: complete Python program
Drop while a condition holds

Chunked

Chunking means cutting a list into pieces of the same size. The last piece may be shorter when the elements do not divide evenly.

This version of Python has no built-in chunk function (newer versions add itertools.batched()), but the helper is one line: range(0, len(items), size) gives the starting indexes 0, 3, 6, … and a slice cuts one piece from each. A slice never reads past the end of the list, so the short last piece comes out right on its own.

The result is a list of lists, and every piece keeps its own brackets: [[0, 1, 2], [3, 4, 5], …].

To work on each piece right away, put the chunks in a comprehension.

Split into chunks of 3

The numbers 0 to 13 are cut into pieces of 3. The last piece has only 2 elements: [12, 13].

Prompt
Write a chunked helper function that cuts a list into pieces of a given size, keeping a shorter last piece. Use it on the numbers 0 to 13 with a size of 3 and print the list of chunks.
Output: complete Python program
Split into chunks of 3

Sum each chunk

The same chunks, but each one is added up with the built-in sum(). Only the sums are kept: [3, 12, 21, 30, 25].

Prompt
Write a chunked helper that cuts a list into pieces of a given size. Use it on the numbers 0 to 13 with a size of 3, then add up each chunk with sum() in a list comprehension and print the list of sums.
Output: complete Python program
Sum each chunk

Windowed

A window is a group of neighbouring elements. Windowing slides the group along the list and collects every position. Unlike chunks, windows overlap.

Python has no built-in windowed. A comprehension with items[i:i + size] does it, and a small windowed helper makes it reusable. With a step, the window moves several positions at a time. With partial set to True, a shorter window at the end is kept instead of being thrown away.

A window of 2 that moves 1 step is the same as pairing each element with the next one. In Python that is the classic idiom zip(words, words[1:]) — and the itertools module has it ready-made as pairwise().

Windows of 3

The window has 3 words and moves 1 position each time. range(len(items) - size + 1) stops when a full window no longer fits, so there are 3 windows. The result is a list of lists, printed on one line.

Prompt
Write a windowed helper function that takes a list and a window size and returns every group of that many neighbouring elements, moving one position at a time. Use it with size 3 on the words one, two, three, four, five and print the result.
Output: complete Python program
Windows of 3

Windows with a step and partial windows

The window has 3 numbers and moves 2 positions each time. Because partial is True, the last window [9, 10] is kept even though it is short.

Prompt
Write a windowed helper that takes a list, a window size, a step and a partial flag. It moves step positions between windows, and when partial is True it also keeps a shorter window at the end. Use it on the numbers 1 to 10 with size 3, step 2 and partial set to True, then print the result.
Output: complete Python program
Windows with a step and partial windows

Sum each window

Every window of 3 neighbours is added up with sum(). Only the sums are kept: [6, 9, 12].

Prompt
Make a list of the numbers 1 to 5. Add up every window of 3 neighbouring elements with sum() in a list comprehension and print the list of sums.
Output: complete Python program
Sum each window

Pair each element with the next one

zip(letters, letters[1:]) pairs each word with its neighbour. Each pair is a tuple, printed in round brackets: ('one', 'two'). itertools.pairwise() gives the same pairs ready-made; here it feeds a comprehension that records whether the first word is longer than the second.

Prompt
Make a list of the words one, two, three, four, five. Pair each element with the one after it using zip over the list and the list without its first element, and print the list of pairs. Then use itertools.pairwise to walk the same neighbours and record whether the first word is longer than the second. Print the list of booleans.
Output: complete Python program
Pair each element with the next one