How to Slice List in Python

Some List Operations

Normal slices

We have already seen how a single value can be accessed with an index from a List. Now we will see how to create another list by splitting a list or in other words how to access another list with more than one value. To do this, you need to write not just one index in [ ] but more than one index with a colon.

   evaluation = [2, 4, 6, 32, 60, 65, 69, 76, 80, 85, 90]

   average = evaluation[4:8]
   print(average)

   good = evaluation[8:]
   print(good)

   bad = evaluation[:4]
   print(bad)

Output

   [60, 65, 69, 76]
   [80, 85, 90]
   [2, 4, 6, 32]

In the above program, we got different sub-lists by slicing them from the evaluation list. For example, first, we took from the fourth index to the eighth index. In the second print, we have taken the elements starting from the eighth index to the end. And in the third print, we have taken the values ​​from the beginning to the fourth index.

When using two indexes on either side of the colon when slicing from the List, the value of the index on the left is included but the value of the index on the right is not included. It's like range.

Index jump

Apart from the beginning and end indexes of the list slices, steps can also be specified. That is, the values in the middle of the mentioned index will be selected but from there the value will be taken by jumping the specified step amount index. Such as -

   serial = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
   print(serial[2:9:3])

Output

   [3, 6, 9]

Here the values between the second and ninth index are taken from the numbers list but jump three steps at a time.

Negative index slice

We have seen how by determining the starting and ending index, slices can be created from a list with some intermediate values. A sliced list is available by setting the starting index of the original list and setting the reverse index from the end - without slicing if you want.

   values = [1, 2, 5, 6, 7, 8, 9, 10, 23, 23, 23, 23]
   print(values[3:-4])

Output

   [6, 7, 8, 9, 10]

With values[3: -4] we have given the starting index for slicing from this list as 3 and the fourth index starting from the end of the list as the last index for slicing.

List reverse

If a negative value is set as a step when creating slices from a List (to the right of the second colon) and all the values in the original list are asked to be selected (not to mention the index on either side of the first colon) then in fact a reverse or reverse list of the original list is created.

Let's look at the following example -

   numbers = [38, 48, 58, 68, 78, 88]
   print(numbers[::-1])

Output

   [88, 78, 68, 58, 48, 38]

Some numeric operations performed on the list

List is a type of data structuring method that allows storing of the integers or the characters in an order indexed by starting from 0. List operations are the operations that can be performed on the data in the list data structure

   numbers = [21, 24, 29, 22, 25, 26, 28]

   # Prints the minimum value among all the elements of the list below
   print(min(numbers))

   # Prints the maximum value among all the elements of the following list
   print(max(numbers))

   # Print sum of all the elements of the following list
   print(sum(numbers))

Output

   21
   29
   175

List slicing is a frequent practice in Python, and it is the most commonly utilized way for programmers to solve efficient problems. Consider a Python list. To access a range of elements in a list, you must slice it. One method is to utilize the simple slicing operator, i.e. colon (:)

With this operator, one can define where to begin slicing, where to terminate slicing and the step. List slicing creates a new list from an old one.

You can find the resources here, Slicing in Python.