Practice
Resources
Contests
Online IDE
New
Free Mock
Events New Scaler
Practice
Improve your coding skills with our resources
Contests
Compete in popular contests with top coders
logo
Events
Attend free live masterclass hosted by top tech professionals
New
Scaler
Explore Offerings by SCALER

Numpy Arrays

Numpy arrays are great alternatives to Python Lists. Some of the key advantages of Numpy arrays are that they are fast, easy to work with, and give users the opportunity to perform calculations across entire arrays.

The array object in NumPy is called ndarray. We can create a NumPy ndarray object by using the array() function.

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

print(my_arr)

print(type(my_arr))
#  It shows that my_arr is numpy.ndarray type.

NumPy Arrays provides the ndim attribute that returns an integer that tells us how many dimensions the array have.

import numpy as np

a = np.array(42)
b = np.array([1, 2, 3, 4, 5])
c = np.array([[1, 2, 3], [4, 5, 6]])
d = np.array([[[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]])

print(a.ndim) # prints 0
print(b.ndim) # prints 1
print(c.ndim) # prints 2
print(d.ndim) # prints 3

Access Array Elements

1-D Array

Array indexing is the same as accessing an array element.

import numpy as np

my_arr = np.array([1, 2, 3, 4])

print(my_arr[0]) # prints 1

2-D Array

To access elements from 2-D arrays we can use comma separated integers representing the dimension and the index of the element.

# Access the 5th element on 2nd dimension:
import numpy as np

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

print('5th element on 2nd dimension: ', my_arr[1, 4]) # prints 10

Negative Indexing

# Use negative indexing to access an array from the end.
import numpy as np

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

print('Last element from 2nd dim: ', my_arr[1, -1]) # prints 10

Searching array

You can search an array for a certain value, and return the indexes that get a match.
To search an array, use the where() method.

my_arr = np.array([1, 2, 3, 4, 5, 4, 4])

x = np.where(my_arr == 4)
print(x) # Finds all the indexes of 4

Try the following example in the editor below.

You are given a list of integers called ‘arr’, convert this into ndarray and use where to find all the occurences of 2 in the array and assign that to x.

Start solving Numpy Arrays on Interview Code Editor
Hints
  • Complete Solution

Discussion


Loading...
Click here to start solving coding interview questions
Free Mock Assessment
Fill up the details for personalised experience.
All fields are mandatory
Current Employer *
Enter company name *
Graduation Year *
Select an option *
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
Phone Number *
OTP will be sent to this number for verification
+1 *
+1
Change Number
Phone Number *
OTP will be sent to this number for verification
+1 *
+1
Change Number
Graduation Year *
Graduation Year *
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
*Enter the expected year of graduation if you're student
Current Employer *
Company Name *
Please verify your phone number
Edit
Resend OTP
By clicking on Start Test, I agree to be contacted by Scaler in the future.
Already have an account? Log in
Free Mock Assessment
Instructions from Interviewbit
Start Test