Python Basic – How to Slice List in Python

Some Extra List Operations

Lists are a collection of items, which can be of different data types such as integers, strings, or other objects. Some extra operations that can be performed on Lists

Normal Slices of List

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.

Example:

   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 –

Example:

   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.

Example:

   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 –

Example:

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

Output:

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

Numeric Operations of List

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

Example:

   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
Share The Tutorial With Your Friends
Twiter
Facebook
LinkedIn
Email
WhatsApp
Skype
Reddit

Check Our Ebook for This Online Course

Advanced topics are covered in this ebook with many practical examples.