Hey Developers, I started my python journey about 3 weeks back and I want to put my knowledge out there for others to learn from.
Today I want to explain what list slicing all about π
This will be exciting...
A list a python data type, (if you want to know more about Python data types let me know in the comments below so that I could publish an article on it)
Back to what a list is a list is used to carry items either stings, characters, integers or float and it is denoted using square brackets "[]"
What is slicing? Slicing is a way to extract certain elements from a list. Extract not remove they are different things.
Example time!!!
my_list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Slicing is done by using the index for each item in the list. There are two ways to get your index you can start from the left to right which will start with positive numbers and O Zero.
my_list =
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
0 1 2 3 4 5 6 7 8 9
The numbers below the list are the indexes
Or it could be negative but it starts from the right to the left with -1
my_list =
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
-10 -9 -8 -7 -6 -5 -4 -3 -2 -1
print(my_list)
To print a specific element in the list you insert the index to the print statement let's say I want to print the element 2 from my list.
For printing single value
It will be -
print(my_list[2])
This is how to print the 2 using it's positive index
For the negative index you'll make use of
print(my_list[-8])
Both of this print statement print
the same output which is the item "2" from the list.
my_list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print(my_list[2])
#Output = 2
#or
my_list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print(my_list[-8])
To extract from a certain range In a list
This is the syntax to make This happen - list[start:stop:step]
We'll start first with the start: stop
Let's say we want to get from 0 to 5
my_list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print(my_list[0:5])
It will be [0:5]
But if you run this it will stop at index 5 which is 4 but me want it to stop at 5 so we'll add 1 to make if 6, [0:6] the output will be- 0, 1, 2, 3, 4, 5. The 0 is the start and the 6 Is the stop
Correct value will be
my_list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print(my_list[0:6])
#output = 0, 1, 2, 3, 4, 5
Let's get from 3 to 7
We'll start with 3 and instead of stopping at 7 we'll add one and we get 8, therefore it's
[3:8]
Output - [3, 4, 5, 6, 7]
Or we can use the negative which is [-7:-2]
To get all the element in the list it will be [1:] cause if you do it this way [1:9]it stops at 8 , therefore it's [1:]
Now we'll make use of the start:stop:step
If we're trying to get a range of 2 till 8
The index of 2 is 2 and 8 is -1 and not -2 for positive we add one negative we minus 1 because if we use -2 we get 9.
And we can make the step 2 that means it will use only 2 steps for 2 to get to 8
It looks like this
my_list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print(my_list[2 : -1 : 2])
Output- 2, 4, 6, 8
To reverse the list we will leave the start and end empty and only our step will be there and the step is -1 since to reverse the list is to begin from behind
It looks this way - [::-1] the output will be every value in the list in a reversed form starting from behind.
my_list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print (my_list[::-1])
#Output = 9, 8, 7, 6, 5, 4, 3, 2, 1, 0
That's all for this π, if you have any questions feel free to ask. Till next time bye!!!
Top comments (4)
Please write an article on the basics of python thanks!
Alright I'll work on it!
Great job
Thank you sir!