Wednesday, 24 January 2024

List slicing: Select and manipulate the elements of a list

List slicing in Python is an effective method for selecting and manipulating particular segments of a list. A slice is placed within square brackets, similar to an index, and consists of three integers separated by colons.

 

Syntax


new_list = original_list[start:stop:step]

 

start (optional): Specifies the starting index for the slice. If omitted, slicing begins from the list's start (index 0).

 

stop (optional): Determines the end index for the slice, not including the element at this index. By default, it's set to the length of the list, indicating that slicing continues up to this point.

 

step (optional): Defines the interval between elements in the slice. The default value is 1, which includes every element. Positive values mean forward iteration, while negative values indicate backward traversal.

 

Let’s experiment with below list.

>>> primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31]
>>> primes
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31]

 

Example 1: Get 2nd, 3rd and 4th elements of a list.

>>> primes[1:4]
[3, 5, 7]

 

Example 2: Get everything from the fourth element to the end

>>> primes[4:]
[11, 13, 17, 19, 23, 29, 31]

Example 3: Get the elements from start to stop with a step size 2.

>>> primes[::2]
[2, 5, 11, 17, 23, 31]

Example 4: Reverse the list (step -1)

>>> primes[::-1]
[31, 29, 23, 19, 17, 13, 11, 7, 5, 3, 2]

 

List slicing creates a new list, without modifying the original.

>>> new_slice = primes[1:4]
>>> 
>>> primes
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31]
>>> 
>>> new_slice
[3, 5, 7]

 

‘new_slice’ is slice form the elements at index 1 to the index 4 (exclusive).

 

As slice creates a new list, updating the values in a slice do not affect the original list. You can confirm the same from below snippet.

>>> new_slice[1] = 37
>>> 
>>> new_slice
[3, 37, 7]
>>> 
>>> primes
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31]

 

Manipulate the elements using list slicing

primes[1:3] = [37, 41]

Above snippet set primes[1] to 37 and primes[2] to 41.

>>> primes
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31]
>>> 
>>> primes[1:3] = [37, 41]
>>> 
>>> primes
[2, 37, 41, 7, 11, 13, 17, 19, 23, 29, 31]

 

Delete the elements using list slicing

‘del primes[1:3]’ delete the elements at indexes 1 and 2.

>>> primes
[2, 37, 41, 7, 11, 13, 17, 19, 23, 29, 31]
>>> 
>>> del primes[1:3]
>>> 
>>> primes
[2, 7, 11, 13, 17, 19, 23, 29, 31]

 

Inserting elements

>>> primes
[2, 7, 11, 13, 17, 19, 23, 29, 31]
>>> 
>>> primes[1:1] = [3, 5]
>>> 
>>> primes
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31]

[1:1]: This is a slice operation. In slicing, the first number (before the colon) is the start index, and the second number (after the colon) is the stop index. Here, both the start and stop indices are 1. This means the slice starts and ends at index 1, effectively selecting a zero-length portion of the list right at index 1.

 

primes[1:1] = [3, 5]

Above snippet add the element 3 and 5 at index 1.

 


Previous                                                 Next                                                 Home

No comments:

Post a Comment