Updated On : Jul-09,2020 Time Investment : ~10 mins

Learning Numpy - Simple Tutorial For Beginners - NumPy - Indexing & Slicing Part 8

Contents of ndarray object can be accessed and modified by indexing or slicing, just like Python's in-built container objects.

As mentioned earlier, items in ndarray object follows zero-based index. Three types of indexing methods are available − field access, basic slicing and advanced indexing.

Basic slicing is an extension of Python's basic concept of slicing to n dimensions. A Python slice object is constructed by giving start, stop, and step parameters to the built-in slice function. This slice object is passed to the array to extract a part of array.

Example 1

import numpy as np
a = np.arange(10)
s = slice(2,7,2)
print (a[s])
[2 4 6]

Example 2

import numpy as np
a = np.arange(10)
b = a[2:7:2]
print (b)
[2 4 6]

Example 3

# slice single item 
import numpy as np

a = np.arange(10)
b = a[5]
print (b)
5

Example 4

# slice items starting from index 
import numpy as np
a = np.arange(10)
print (a[2:])
[2 3 4 5 6 7 8 9]

Example 5

# slice items between indexes 
import numpy as np
a = np.arange(10)
print (a[2:5])
[2 3 4]

Example 6

import numpy as np
a = np.array([[1,2,3],[3,4,5],[4,5,6]])
print (a)

# slice items starting from index
print ('Now we will slice the array from the index a[1:]')
print (a[1:])
[[1 2 3]
 [3 4 5]
 [4 5 6]]
Now we will slice the array from the index a[1:]
[[3 4 5]
 [4 5 6]]

Example 7

# array to begin with 
import numpy as np
a = np.array([[1,2,3],[3,4,5],[4,5,6]])

print ('Our array is:')
print (a)
print ('\n')

# this returns array of items in the second column 
print ('The items in the second column are:')
print (a[...,1])
print ('\n')

# Now we will slice all items from the second row 
print ('The items in the second row are:')
print (a[1,...])
print ('\n')

# Now we will slice all items from column 1 onwards 
print ('The items column 1 onwards are:')
print (a[...,1:])
Our array is:
[[1 2 3]
 [3 4 5]
 [4 5 6]]


The items in the second column are:
[2 4 5]


The items in the second row are:
[3 4 5]


The items column 1 onwards are:
[[2 3]
 [4 5]
 [5 6]]
Dolly Solanki  Dolly Solanki

YouTube Subscribe Comfortable Learning through Video Tutorials?

If you are more comfortable learning through video tutorials then we would recommend that you subscribe to our YouTube channel.

Need Help Stuck Somewhere? Need Help with Coding? Have Doubts About the Topic/Code?

When going through coding examples, it's quite common to have doubts and errors.

If you have doubts about some code examples or are stuck somewhere when trying our code, send us an email at coderzcolumn07@gmail.com. We'll help you or point you in the direction where you can find a solution to your problem.

You can even send us a mail if you are trying something new and need guidance regarding coding. We'll try to respond as soon as possible.

Share Views Want to Share Your Views? Have Any Suggestions?

If you want to

  • provide some suggestions on topic
  • share your views
  • include some details in tutorial
  • suggest some new topics on which we should create tutorials/blogs
Please feel free to contact us at coderzcolumn07@gmail.com. We appreciate and value your feedbacks. You can also support us with a small contribution by clicking DONATE.


Subscribe to Our YouTube Channel

YouTube SubScribe

Newsletter Subscription