While programming in python, lists are one of the most common datatypes you’ll find yourself using. There are various operations that you can perform on a list, like accessing the elements of a list, appending to a list, updating a list, and so on. You might have even come across a code that includes “for x in A[1:]” and wonder what it means. By the end of the article, you will have an obvious idea of what it means. But first, let’s look at the basics.
What is a list?
Lists are one of the built-in datatypes in python that store multiple elements under the same variable. List elements are ordered, mutable (changes can be made), and also can have duplicate values. The elements of a list can be of any datatype like strings, int, boolean, etc.
A significant point to note is those list elements are indexed, meaning every element in the list is assigned a unique integer value representing its position. This value is called the index of the element. The first element has an index , and the last element is an index [n-1], where n is the size of the list.
Example of a list:
The above code creates a list of integers called ‘my_list.’ We use square brackets with lists and commas to separate the list elements.
List Slicing Explained
Accessing elements of a list is simple. Consider that we want to access the element at index 3 in the above list. We can do it by using the following code:
But what if we wish to access a subset of the list’s elements? For example, the first three items on the list, the last two, or even every other item. We can do this very quickly by using the slicing operator (:).
The syntax of the slicing operator is as follows:
Here, ‘start’ is the index from where we start slicing (it is included in the slice), ‘stop’ is the index of the last element to include in the slice (it is not included excluded in the slice), and ‘step’ is the interval between the elements to be selected from the list.
The default values of ‘start,’ ‘end,’ and ‘step’ are 0, ‘n,’ and 1, respectively, where n is the size of the list. If any values are not mentioned in a slice, default values are assumed.
[1, 3, 5]
Here, we start slicing from index zero to the last index with a step of 2. In the output, we can notice that every other element is printed.
Also read: What Is a List and How to Split the Elements of a List?
Iterate Over a Sliced List
We can iterate over a list using various methods like using for loops, while loops, list comprehension, and so on. We will only be covering ‘for’ loops for now.
for i in my_list: print(i)
Here, the code iterates through each element ‘i’ in the ‘my_list’ and prints the element ‘i’ at each iteration.
Now that we have looked into lists, list slicing, and iterating through lists, the code “for x in A[1:]” probably makes more sense.
Looking closely at the syntax of “for x in A[1:]”, you’ll notice that it’s a for loop for iterating through a sliced list. Here, the ‘start’ value is 1 meaning the first element of the list is skipped in the slice. The ‘stop’ and ‘step’ values are omitted, meaning the slicing is done till the end of the list, and the step size is 1, which means no element is skipped in between.
Let’s look at an example to make it more clear.
A = [1, 2, 3, 4, 5] for x in A[1:]: print(x)
2 3 4 5
In the above code, we are iterating through a sliced list. First, we obtain the sliced list and then we iterate through the obtained sliced list using the for loop and print the corresponding element at each iteration.
Let’s look at another example:
A = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] for x in A[2:8:2]: print(x)
3 5 7
Here, the ‘start,’ ‘stop,’ and ‘step’ are 2, 8, and 2, respectively, once the list is sliced, for loop iterates over the sliced list printing each element.
Iterate through a sliced list in reverse order
Slicing can also be done using negative indexes. Let’s take a list, ‘my_list=[1,2,3]’.
Here, -1 is the index of the last element and -n is the index of the first element where n is the size of the list. So, in this example, -3 is the index of the first element.
Using negative indexes we can iterate through a list in reverse order. Say there is a list of integers called ‘A’ and we have to iterate through the list in reverse leaving the last two elements. How do we do this?
A = [1, 2, 3, 4, 5] for x in A[-3::-1]: print(x)
3 2 1
In the above code, we use negative index slicing where ‘start’ is -3 which means the sliced list starts from the third last element excluding the last two elements. Here ‘stop’ is omitted so it assumes the default value which in the case of negative indexing would be the negative of size of the list. The list size is 5. So ‘stop’ has the value of -5. The ‘step’ value is -1 which means that slice takes every element of the list from the ‘start’ index till the end but in reverse order.
Also read: What is the meaning of “int(a[::-1])” in Python?
With this we have nicely understood what the code “for x in A[1:]” means by first understanding lists and then looking at list slicing. We then learnt how to iterate through a sliced list using a for loop and also covered negative indexes and how to iterate through a sliced list in reverse.