0% found this document useful (0 votes)
32 views

Array (Slicing)

Slicing in Python allows extracting elements from an array starting at one index up to another index. Slicing can be done on 1D and 2D arrays using the syntax [start:end:step] to define the start, end and step indexes. If start or end are not provided, they default to 0 and the array length respectively. Negative indexes can also be used to slice from the end. Slicing enables extracting contiguous or non-contiguous elements from arrays.

Uploaded by

Saurabh Kale
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

Array (Slicing)

Slicing in Python allows extracting elements from an array starting at one index up to another index. Slicing can be done on 1D and 2D arrays using the syntax [start:end:step] to define the start, end and step indexes. If start or end are not provided, they default to 0 and the array length respectively. Negative indexes can also be used to slice from the end. Slicing enables extracting contiguous or non-contiguous elements from arrays.

Uploaded by

Saurabh Kale
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Array (Slicing)

Slicing arrays
Slicing in python means taking elements from one given index to another given
index.
We pass slice instead of index like this: [start:end].
We can also define the step, like this: [start:end:step].
If we don't pass start its considered 0
If we don't pass end its considered length of array in that dimension
If we don't pass step its considered 1

#Slice elements from index 1 to index 5 from the following array:

import numpy as np

arr = np.array([1, 2, 3, 4, 5, 6, 7])

print(arr[0:3])

[1 2 3]

#Slice elements from index 4 to the end of the array:

import numpy as np

arr = np.array([1, 2, 3, 4, 5, 6, 7])

print(arr[4:])

[5 6 7]

#Slice elements from the beginning to index 4 (not included):

import numpy as np

arr = np.array([1, 2, 3, 4, 5, 6, 7])

Array (Slicing) 1
print(arr[:4])

[1 2 3 4]

#Negative Slicing
#Use the minus operator to refer to an index from the end:

import numpy as np

arr = np.array([1, 2, 3, 4, 5, 6, 7])

print(arr[-3:-1])

[5 6]

#STEP
#Use the step value to determine the step of the slicing:
#Return every other element from the entire array:

import numpy as np

arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])

print(arr[0::3])

[ 1 4 7 10]

#Return every other element from index 1 to index 5:

import numpy as np

arr = np.array([1, 2, 3, 4, 5, 6, 7])

print(arr[0:7:2])

[1 3 5 7]

#Slicing 2-D Arrays


#From the second element, slice elements from index 1 to index 4 (not included):

Array (Slicing) 2
import numpy as np

arr = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10],[1,2,3,4,5]])

print(arr[0:2, 1:4])

[[2 3 4]
[7 8 9]]

#From both elements, return index 2:

import numpy as np

arr = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10],[2,3,4,5,6]])

print(arr[0:3, 2:4])

[[3 4]
[8 9]
[4 5]]

#From both elements, slice index 1 to index 4 (not included), this will return a 2-D
array:

import numpy as np

arr = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])

print(arr [0:2,1:4])

[[2 3 4]
[7 8 9]]

Array (Slicing) 3

You might also like