10.7. List SlicesΒΆ
The slice operation we saw with strings also work on lists. Remember that the first index is the starting point for the slice and the second number is one index past the end of the slice (up to but not including that element). Recall also that if you omit the first index (before the colon), the slice starts at the beginning of the sequence. If you omit the second index, the slice goes to the end of the sequence.
Before you keep reading...
Making great stuff takes time and $$. If you appreciate the book you are reading now and want to keep quality materials free for other students please consider a donation to Runestone Academy. We ask that you consider a $10 donation, but if you can give more thats great, if $10 is too much for your budget we would be happy with whatever you can afford as a show of support.
Check your understanding
- [ [ ], 3.14, False]
- Yes, the slice starts at index 4 and goes up to and including the last item.
- [ [ ], 3.14]
- By leaving out the upper bound on the slice, we go up to and including the last item.
- [ [56, 57, "dog"], [ ], 3.14, False]
- Index values start at 0.
What is printed by the following statements?
alist = [3, 67, "cat", [56, 57, "dog"], [ ], 3.14, False]
print(alist[4:])