SLICING A PYTHON LIST

in Steemit Nursery3 years ago


SLICING.png

Hello Everyone. Welcome to my next part tutorial on python list. This will be the final part on working with python list. If you want to see the previous tutorial, you may visit the below link:

Introduction to List
Modifying a List

List slicing means creation of a new list from the existing list containing the specified or requested items. Python provides us an easy method for slicing a list with easy-to-use syntax. You will need to specify the index number of first element, from where you want to slice and the index number of last element, where you want to end your slice. For example, let's consider this numerical list of a random number as below:

number_list = [5, 54, 74, 28, 8, 44, 92, 38, 73, 20, 2, 35, 16, 88, 65]

If we want to slice the first four number from this list, we will write the name of the list and in the square bracket, the index number of first element i.e. 0 and the index number of last element i.e. 4, with them separated by ":". Always note that the sliced list won't print the value in the last index number you have specified. It will end its loop there and won't print the value.

number_list = [5, 54, 74, 28, 8, 44, 92, 38, 73, 20, 2, 35, 16, 88, 65]

print(number_list[0:4])

Here, you will get value upto 28 i.e. index number 3 and won't get value 8 at the end of the list. Notice the output below:

image.png

However you don't need to specify the starting of the index number during slicing if you are starting from index number 0. For the above code, it can be written like this too:

number_list = [5, 54, 74, 28, 8, 44, 92, 38, 73, 20, 2, 35, 16, 88, 65]

print(number_list[:4])

Python will automatically know that your index number is 0. If you want to slice from 54 till 92 , you now need to write this syntax: number_list[1:7]. I hope you now grasp the concept of slicing easily.

image.png

In the same way, if you want to slice a list starting from 8 i.e. index number 4 all the way to the end the syntax would be: number_list[4:]. You don't need to specify the index number of your last element for this. Lets see the output for this one:

image.png

You can also slice a list with the help of the negative number too. See this code for your reference:

number_list = [5, 54, 74, 28, 8, 44, 92, 38, 73, 20, 2, 35, 16, 88, 65]

print(number_list[-4:])

Here the slicing starts from the end of the list and the last element is indexed at -1. In the same way, 88 at -2, 16 at -3 and 35 at -4. So the output will be [35, 16, 88, 65].

image.png

Ok so now what about if we slice a list with this syntax number_list[:-4]. It will tell python to start from the index 0 all the way to index number -4 from the back as we just discussed above. Below is the output:

image.png

Till now we have sliced a list in a series without excluding any elements in between. Lets say you want to create a new list from the above original numerical list but this time you want to omit the element in between two elements. You want 5 as first element then omit 54 then 74 as second element and then again omit 28 and then 8 as third element. To do this we use the following slicing syntax.

number_list = [5, 54, 74, 28, 8, 44, 92, 38, 73, 20, 2, 35, 16, 88, 65]

print(number_list[0:8:2])

The first element starts from index 0 and the last element ends in index 8 and we want to have a gap of 2 between each element in new list. The output now can be seen as:

image.png

See my introduction post here.

Sort:  
 3 years ago 

Excelente información. Éxitos

 3 years ago 

Thank you :)

Coin Marketplace

STEEM 0.29
TRX 0.12
JST 0.033
BTC 62559.43
ETH 3092.10
USDT 1.00
SBD 3.86