# Python - Variable list slicing

In this article we will learn about variable list slicing. As you know List slicing is very useful and powerful feature of Python using which we can cut out some specific portions of any list quickly. Python provides us various techniques and methods for slicing and extracting the elements based on some specific criteria or patterns.

Take an example to understand −

list number: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 33, 34, 56, 43, 67]


We have list of some random numbers and suppose we want to slice some of the portion from the list from index 8 to end of the list then the output will come out as −

Sliced list number: [9, 10, 33, 34, 56, 43, 67]


Let’s look at different methods using which we can perform this task.

## Method 1: Basic Slicing Technique

In the basic list slicing technique we use simple python syntax which is list[start:end:step]. This can extract some portion of any list from the start index to the end index with any given step value which is optional. Here's an example

### Example

num = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 33, 34, 56, 43, 67]
sliced_number = num[2:15:2]

print("Original list number: ", num)
print("Sliced list number : ", sliced_number)


### Output

Original list number: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 33, 34, 56, 43, 67]
Sliced list number : [3, 5, 7, 9, 33, 56, 67]


### Explanation

Here in the above example, we are slicing from index 2 to index 15 (exclusive), and moving step by 2.

## Method 2: Using List Comprehension

Here we will use the list comprehension technique and we will also specify some condition while slicing the list. Lets see using an example.

### Example

number = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 33, 34, 56, 43, 67]
sliced_number = [num for num in number[2:9] if num % 2 == 0]

print("Original list number: ", number)
print("Sliced list number : ", sliced_number)


### Output

Original list number: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 33, 34, 56, 43, 67]
Sliced list number : [4, 6, 8]


### Explanation

Here in the above example we used the list comprehension technique to slice the list from index 2 to 9. We have a list named as number. We used the condition as num%2==0 which will filter the values which are divided by the element 2. So our final output will come as [4, 6, 8] as its element is divided by 2.

## Method 3: Slice From Beginning

In this method we will slice the list from beginning without specifying the starting index, we can also leave the start value.

### Example

num = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 33, 34, 56, 43, 67]
sliced_number = num[:6]

print("Original list number: ", num)
print("Sliced list number : ", sliced_number)


### Output

Original list number: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 33, 34, 56, 43, 67]
Sliced list number : [3, 5, 7, 9, 33, 56, 67]


### Explanation

Here in the above example we specified the number as 6 so, it will slice the list from the start to the index 6 (including). If you will specify the number as 4 so it will slice the first 4 elements from starting.

## Method 4: Slice Till the End

In this method we will slice the list from any point in the list till end of the list without specifying the end index.

### Example

num = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 33, 34, 56, 43, 67]
sliced_number = num[8:]

print("Original list number: ", num)
print("Sliced list number : ", sliced_number)


### Output

Original list number: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 33, 34, 56, 43, 67]
Sliced list number : [9, 10, 33, 34, 56, 43, 67]


### Explanation

Here in the above example, we slice the list starting from index 8 and go until the end. So we get output as the last 7 numbers of the list. If we specify the number as 4 then we will get the last 3 numbers of the list.

## Method 5: Skipping Elements

In this method we will see how we can skip the elements in the we can skip elements in a list using slicing by specifying a step value.

### Example

num = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 33, 34, 56, 43, 67]
skipped_number_list = num[::2]

print("Original list number: ", num)
print("Skipped number list : ", skipped_number_list)


### Output

Original list number: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 33, 34, 56, 43, 67]
Skipped number list : [1, 3, 5, 7, 9, 33, 56, 67]


### Explanation

Here in the above example we slice the list with a step of 2, extracting every second element from the list. If we specify the step as 3 so it will print the element by skipping every 2 elements.

## Method 6: Negative Indexing

In this method we will use the negative index to slice the variable list. We use negative indexing to count from the end of the list.

### Example

num = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 33, 34, 56, 43, 67]
sliced_number = num[-8:-2]

print("Original list number: ", num)
print("Sliced list number : ", sliced_number)


### Output

Original list number: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 33, 34, 56, 43, 67]
Sliced list number : [8, 9, 10, 33, 34, 56]


### Explanation

Here in the above example we used the negative indexing to slice the list. We specified the indexing as -8 which is 8th index from the end to -2 which is 2nd index from end. So the list will get sliced from the 8th till 2nd element from the end. If we specify the index as -5 to -1 then the list will be [33, 34, 56, 43].

## Conclusion

So list slicing is very important task we use in the daily life of programming. In this article we saw different methods like slice from beginning, list comprehension, from end, skipping element which provides us with different techniques to slice the list. You can use any one of them according to your requirement. But having knowledge about different techniques is very crucial for learning purposes.

Updated on: 06-Oct-2023

89 Views