Python Crash Course
)}

Slicing

In the last lesson, we looked at the List data type which contains a sequence of elements enclosed in brackets [ ] .

These lists are ordered, and the elements in those lists were accessed based on their index. In this article, we will explore a way of accessing more than one element from a list by slicing.

Slicing is done with a set of indices which specifies the start range and the stop range in a list.

The start specifies the index where we want to start slicing the list. The stop index specifies the index at which we want to stop slicing.

Let's assume a List from 0 - 9 with:

  • Start Index: 2
  • Stop Index: 7

Notice how we ended at 6 and not 7. That's because the upper index is exclusive, meaning the slicing stops at that index minus 1. Therefore, it's not included in our sliced segment. The index will start at 2 , since the start index is inclusive and will end at 6, since the stop index is exclusive.

L‍et's try this out:

o‍rdered_numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

L‍et's assume that I want to grab elements from 1 to 8, we will use slicing to demonstrate how to do that.

sliced_list = ordered_numbers[1:9] print(sliced_list)

‍>> [1, 2, 3, 4, 5, 6, 7, 8]

Starting from the beginning of the List

‍Now, what if we want to access the value from 0 to 8 ? We would choose a start index of 0 and a stop index of 9 , like so:

o‍rdered_numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] sliced_list = ordered_numbers[0:9] print(sliced_list)

‍‍>> [0, 1, 2, 3, 4, 5, 6, 7, 8]

Alternatively, you can omit the start index, and python will assume that you would like to start from the beginning:

o‍rdered_numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] sliced_list = ordered_numbers[:9] print(sliced_list)

‍‍>> [0, 1, 2, 3, 4, 5, 6, 7, 8]

Accessing everything until the end of the list

‍Now if you would like to access all the elements from a start index until the end of the list, you can simply omit the stop index:

o‍rdered_numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] sliced_list = ordered_numbers[2:] print(sliced_list)

‍>> [2, 3, 4, 5, 6, 7, 8, 9, 10]

Slicing Strings

A string in python can be also be considered as a list of characters.

show_title = 'game of thrones'

You can access the third index of the string the same way you would do so in a list

print( show_title[3] )

>> m

you can also slice a string just like lists

print(show_title[2:12])

>> me of thro

Bonus

Creating a list of numbers using range

‍So far we manually tried to create a list of numbers from 0 - 10. But, what if I asked you to create a range of values from 0 - 1000. You will find that doing so will take a long time. This is why we will make use of the range function.

range( start , stop , step )

range takes three arguments start, stop, and step.

  • ‍The start argument determines the start index of your list.
  • The stop argument determines the stop index of your list.
  • The step argument determines the step size of your list.

L‍et's try it out:

long_list_of_numbers = list( range(5, 21) ) print(long_list_of_numbers)

‍>> [5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]

t‍he start index is inclusive, therefore it shows in the list, but the stop index is exclusive, and the value that precedes it will be used in the list.

Omitting the first argument

we can also omit the first variable, and our list will be created from 0 until the stop index

long_list_of_numbers = list( range(21) ) print(long_list_of_numbers)

‍‍>> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]

this produces a list of numbers from 0 to 20.

Using the third argument step

We can also use the final argument stepto indicate the step size of our range.

long_list_of_numbers = list( range(0, 21, 2) ) print(long_list_of_numbers)

‍>> [0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20]

T‍he execution above has created a list between 0 and 21, but it only includes each second number in the sequence.

Feedback Summary
4.8
8 students
5

75%
4

25%
3

0%
2

0%
1

0%
Written Reviews
There are no written reviews yet.