Pyton

Indexing and Slicing in Python Tutorial

Indexing and Slicing in Python Tutorial
List is considered to be a useful feature of Python. It is handy and can be accessed using the index value of the list. Moreover, it is written inside the squared brackets. In this article, we will illustrate and check these methods of slicing and indexing in Python using Python 3.8 version.

What is Indexing?

Indexing is creating a reference to an element of an iterable (tuples, strings) by its particular position within the iterable.

To check out how to access individual elements of a list, we will create a list at first. We will see how the sequences of objects work within the list in Python. The list of objects is written within the square brackets, as shown below:

>>> mylist[m]

Here, mylist is the name of the list, and [m] is an array having the list of all elements that exist within this array.

Creating a list in Python

To create a list in Python, define the list, and then enter the elements of the string in the array. Here is the name of the list and its elements, apple, egg, mango, tomato, banana, and bread. The general syntax for creating a list is:

>>> listname = ['element1', 'element2', 'element3', 'element4', 'element5', 'element6']

Example:

The list may have as many elements as per the requirement of the user.

Accessing a Particular Index

List indexing in Python is based on the zero index and starts from zero, then goes on. The technique is similar to the indexing in the case of data sets. To display the list, simply type the name of the list and then hit enter. The list will be displayed, as shown in the appended figure. Then we will call the zero index of the list, then the 2nd, and then the 5th index.

The syntax will be:

>>> mylist[0] - Will display the zeroth index of the list
>>> mylist[2] - Will display the second index of the list
>>> mylist[5] - Will display the fifth index of the list

To display the last element of the list in Python, we will use:

>>> mylist[len(mylist)-1]

The last element in the list will be displayed.

If you try to call out the element that is not included in the list, it will display an error. For example, here on the list, we do not have any index after 5, so if we try to call the 6th index, it will return an error.

>>> mylist[6] - Will return error since our list is only until the 5th index.

Accessing the Negative List Index

Users can also access the index in the negative list. Negative in indexing implies starting of the list from -1, followed by -2, then -3, and so on.

The syntax to be used is:

>>> mylist[-1] - Will display the values of the last index from the list
>>> mylist[-2] - Will display the values of the second last index from the list
>>> mylist[-3] - Will display the values of the third last index from the list
>>> mylist[-4] - Will display the values of the fourth last index from the list
>>> mylist[-5] - Will display the values of the fifth last index from the list
>>> mylist[-6] - Will display the values of the sixth last index from the list

Here, again, if we try to call out the -7th or -8th index it will return an error since our list is still until the 5th index and has no value after it.

What is Slicing?

Slicing is a subset of all elements from an iterable (tuples, strings) based on their indexes. Here, we extract the portion of a particular list and then make the list return into that section. For example, if we see:

>>> mylist[m:n]

It will return a portion of mylist. It will start with point m up to n, but excluding the n value. We can also use negative index values here.

Slicing a List

In the example, we have the list a, and for a[2:5], it will return the values from 2nd index until the 4th. As discussed earlier, it will exclude the value of the 5th index in this example.

In the next example, a[-5:-2], we checked the negative index value that has returned the index values from the -5 index to the -2 index.

And in a[1:4], it returned the index values from 1 to 3 by excluding the 4th index value.

Making the value of a [-5:2] == a[1:4]

Omitting the Index

Users can omit the first index, a[:n], and begin the slice at the start of the list, or users can omit the last index, a[m:], that will extend the slice from the first index (m) up to the list end. In case users omit both indexes a[:], it will then return a copy of the entire list. Let's check it out with examples.

In a [:4], we sliced the list until the 4th index, and it will include all values from zero index until the 3rd.

Next, for a[0:4], we started the index value at zero, which is similar to the previous [:4] case, but here, we have specified that it started from 0, which is indifferent, and both will return the same results.

In the next example, we have a[2:], this will display all values from the 2nd index place until the end of the list, and in the a[2:len(a)] example, we will again get the same results.

Stride

To check out the stride feature, we will add a third index. It is a step that is used in the slice notation. Stride value can either be positive or negative.

The example displays the stride value 2 in a[0:6:2], i.e., from the list, it allows us to choose only three elements starting from 0 index.

The example displays the stride value 2 in a[1:6:2], i.e., from the list, it allows us to choose only three elements starting from 1 index.

The example displays the stride value -2 in a[6:0:-2], i.e., from the list, it allows us to choose only three elements from the end of the list.

Conclusion

In this tutorial, we went through the ways of using indexing and slicing in the Python language. It is one of the major features that can be used by all programmers for the ease of their data formulation.

Add Mouse gestures to Windows 10 using these free tools
In recent years computers and operating systems have greatly evolved. There was a time when users had to use commands to navigate through file manager...
Control & manage mouse movement between multiple monitors in Windows 10
Dual Display Mouse Manager lets you control & configure mouse movement between multiple monitors, by slowing down its movements near the border. Windo...
WinMouse lets you customize & improve mouse pointer movement on Windows PC
If you want to improve the default functions of your mouse pointer use freeware WinMouse. It adds more features to help you get the most out of your h...