The Wayback Machine - https://web.archive.org/web/20240927063902/https://www.geeksforgeeks.org/python-arrays/
Open In App

Python Arrays

Last Updated : 12 Sep, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

For simplicity, we can think of a Python array as a fleet of stairs where on each step is placed a value (let’s say one of your friends). Here, you can identify the location of any of your friends by simply knowing the count of the steps they are on. The array can be handled in Python by a module named array. They can be useful when we have to manipulate only specific data type values. A user can treat lists as arrays. However, the user cannot constrain the type of elements stored in a list. If you create Python arrays using the array module, all elements of the array in Python must be of the same type. In this article, we will see how to use an array in Python with examples.

What is an Array in Python?

An array is a collection of items stored at contiguous memory locations. The idea is to store multiple items of the same type together. This makes it easier to calculate the position of each element by simply adding an offset to a base value, i.e., the memory location of the first element of the array (generally denoted by the name of the array).

To get a detailed explanation of Python Array go through the Python Self Paced course , this course offers you a complete fundamental of Python Array.

Python Arrays

Create an Array in Python

Array in Python can be created by importing an array module. array( data_type , value_list ) is used to create array in Python with data type and value list specified in its arguments.

In below code Python create array : one of integers and one of doubles . It then prints the contents of each array to the console.

Python
import array as arr
a = arr.array('i', [1, 2, 3])
print("The new created array is : ", end=" ")
for i in range(0, 3):
    print(a[i], end=" ")
print()
b = arr.array('d', [2.5, 3.2, 3.3])
print("\nThe new created array is : ", end=" ")
for i in range(0, 3):
    print(b[i], end=" ")

Output
The new created array is :  1 2 3 

The new created array is :  2.5 3.2 3.3 

Complexities for Creation of Arrays:

Time Complexity: O(n)
Auxiliary Space: O(n)

Some of the data types are mentioned below which will help in create array in Python 3.8 of different data types.

Create an Array in Python


Now we will see how to use array in Python 3.8 with example.

Adding Elements to an Array

Elements can be added to the Python Array by using built-in insert() function. Insert is used to insert one or more data elements into an array. Based on the requirement, a new element can be added at the beginning, end, or any given index of array. append() is also used to add the value mentioned in its arguments at the end of the Python array.

Below, code first imports the array module as Python import array as arr . Then, it creates an array of integers named a with elements [1, 2, 3] . In below code Python print array as array is printed before and after inserting the integer 4 at index 1. Similarly, an array of doubles named b with elements [2.5, 3.2, 3.3] is created and printed before and after appending the double 4.4 to the array.

Python
import array as arr
a = arr.array('i', [1, 2, 3])
print("Array before insertion : ", end=" ")
for i in range(0, 3):
    print(a[i], end=" ")
print()
a.insert(1, 4)
print("Array after insertion : ", end=" ")
for i in (a):
    print(i, end=" ")
print()
b = arr.array('d', [2.5, 3.2, 3.3])
print("Array before insertion : ", end=" ")
for i in range(0, 3):
    print(b[i], end=" ")
print()
b.append(4.4)
print("Array after insertion : ", end=" ")
for i in (b):
    print(i, end=" ")
print()

Output
Array before insertion :  1 2 3 
Array after insertion :  1 4 2 3 
Array before insertion :  2.5 3.2 3.3 
Array after insertion :  2.5 3.2 3.3 4.4 

Complexities for Adding elements to the Arrays

Time Complexity: O(1)/O(n) ( O(1) – for inserting elements at the end of the array, O(n) – for inserting elements at the beginning of the array and to the full array
Auxiliary Space: O(1)

Accessing Elements from the Array

In order to access the array items refer to the index number. Use the index operator [ ] to access an item in a array in Python. The index must be an integer.

Below, code shows first how to Python import array and use of indexing to access elements in arrays. The a[0] expression accesses the first element of the array a , which is 1. The a[3] expression accesses the fourth element of the array a , which is 4. Similarly, the b[1] expression accesses the second element of the array b , which is 3.2, and the b[2] expression accesses the third element of the array b , which is 3.3.

Python
import array as arr
a = arr.array('i', [1, 2, 3, 4, 5, 6])
print("Access element is: ", a[0])
print("Access element is: ", a[3])
b = arr.array('d', [2.5, 3.2, 3.3])
print("Access element is: ", b[1])
print("Access element is: ", b[2])

Output
Access element is:  1
Access element is:  4
Access element is:  3.2
Access element is:  3.3

Complexities for accessing elements in the Arrays

Time Complexity: O(1)
Auxiliary Space: O(1)

Removing Elements from the Array

Elements can be removed from the Python array by using built-in remove() function but an Error arises if element doesn’t exist in the set. Remove() method only removes one element at a time, to remove range of elements, iterator is used. pop() function can also be used to remove and return an element from the array, but by default it removes only the last element of the array, to remove element from a specific position of the array, index of the element is passed as an argument to the pop() method.
Note – Remove method in List will only remove the first occurrence of the searched element.

Below, code shows how to Python import array, how to create, print, remove elements from, and access elements of an array in Python. It imports the array module, which is used to work with arrays. It creates an array of integers in and Python print arrays or prints the original array. It then removes an element from the array and prints the modified array. Finally, it removes all occurrences of a specific element from the array and prints the updated array

Python
import array
arr = array.array('i', [1, 2, 3, 1, 5])
print("The new created array is : ", end="")
for i in range(0, 5):
    print(arr[i], end=" ")

print("\r")
print("The popped element is : ", end="")
print(arr.pop(2))
print("The array after popping is : ", end="")
for i in range(0, 4):
    print(arr[i], end=" ")

print("\r")
arr.remove(1)
print("The array after removing is : ", end="")
for i in range(0, 3):
    print(arr[i], end=" ")

Output
The new created array is : 1 2 3 1 5 
The popped element is : 3
The array after popping is : 1 2 1 5 
The array after removing is : 2 1 5 

Complexities for Removing elements in the Arrays

Time Complexity: O(1)/O(n) ( O(1) – for removing elements at the end of the array, O(n) – for removing elements at the beginning of the Python create array and to the full array
Auxiliary Space: O(1)

Slicing of an Array

In Python array, there are multiple ways to print the whole array with all the elements, but to print a specific range of elements from the array, we use Slice operation . Slice operation is performed on array with the use of colon(:). To print elements from beginning to a range use [:Index], to print elements from end use [:-Index], to print elements from specific Index till the end use [Index:], to print elements within a range, use [Start Index:End Index] and to print whole List with the use of slicing operation, use [:]. Further, to print whole array in reverse order, use [::-1].

This code employs slicing to extract elements or subarrays from an array. It starts with an initial array of integers and creates an array from the list. The code slices the array to extract elements from index 3 to 8, from index 5 to the end, and the entire array and In below code Python print array as The sliced arrays are then printed to demonstrate the slicing operations.

Python
import array as arr
l = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

a = arr.array('i', l)
print("Initial Array: ")
for i in (a):
    print(i, end=" ")
Sliced_array = a[3:8]
print("\nSlicing elements in a range 3-8: ")
print(Sliced_array)
Sliced_array = a[5:]
print("\nElements sliced from 5th "
      "element till the end: ")
print(Sliced_array)
Sliced_array = a[:]
print("\nPrinting all elements using slice operation: ")
print(Sliced_array)

Output
Initial Array: 
1 2 3 4 5 6 7 8 9 10 
Slicing elements in a range 3-8: 
array('i', [4, 5, 6, 7, 8])

Elements sliced from 5th element till the end: 
array('i', [6, 7, 8, 9, 10])

Printing all elements...

Searching Element in an Array

In order to search an element in the array we use a python in-built index() method. This function returns the index of the first occurrence of value mentioned in arguments.

Example: The code demonstrates how to create array in Python, print its elements, and find the indices of specific elements. It imports the array module, creates an array of integers, prints the array using a for loop, and then uses the index() method to find the indices of the first occurrences of the integers 2 and 1.

Python
import array
arr = array.array('i', [1, 2, 3, 1, 2, 5])
print("The new created array is : ", end="")
for i in range(0, 6):
    print(arr[i], end=" ")

print("\r")
print("The index of 1st occurrence of 2 is : ", end="")
print(arr.index(2))
print("The index of 1st occurrence of 1 is : ", end="")
print(arr.index(1))

Output
The new created array is : 1 2 3 1 2 5 
The index of 1st occurrence of 2 is : 1
The index of 1st occurrence of 1 is : 0

Complexities for searching elements in the Arrays

Time Complexity: O(n)
Auxiliary Space: O(1)

Updating Elements in an Array

In order to update an element in the array we simply reassign a new value to the desired index we want to update.

Example: This code illustrates the functionality of modifying elements within an array using indexing. It imports the array module, creates an array of integers, and prints the initial array. Then, it modifies two elements of the array at specific indexes and prints the updated array. This serves to demonstrate how indexing allows for dynamic manipulation of array contents.

Python
import array
arr = array.array('i', [1, 2, 3, 1, 2, 5])
print("Array before updation : ", end="")
for i in range(0, 6):
    print(arr[i], end=" ")

print("\r")
arr[2] = 6
print("Array after updation : ", end="")
for i in range(0, 6):
    print(arr[i], end=" ")
print()
arr[4] = 8
print("Array after updation : ", end="")
for i in range(0, 6):
    print(arr[i], end=" ")

Output
Array before updation : 1 2 3 1 2 5 
Array after updation : 1 2 6 1 2 5 
Array after updation : 1 2 6 1 8 5 

Complexities for updating elements in the Arrays

Time Complexity: O(1)
Auxiliary Space: O(1)

Different Operations on Python Arrays

Counting Elements in an Array

In order to count elements in an array we need to use count method.

Example: The code demonstrates how to determine the frequency of a particular element within an array. It imports the array module, creates an array of integers, to create arrays in Python and counts the occurrences of the number 2 using the count() method, and finally prints the result. This code snippet effectively showcases the ability to analyze the distribution of elements in arrays.

Python
import array
my_array = array.array('i', [1, 2, 3, 4, 2, 5, 2])
count = my_array.count(2)
print("Number of occurrences of 2:", count)

Output
Number of occurrences of 2: 3

Complexities for counting elements in the Arrays

Time Complexity: O(n)
Auxiliary Space: O(1)

Reversing Elements in an Array

In order to reverse elements of an array we need to simply use reverse method.

Example: The presented code demonstrates the functionality of reversing the order of elements within an array using the reverse() method. It imports the array module, creates an array of integers, prints the original array, reverses the order of elements using reverse() , and then prints the reversed array. This effectively illustrates the ability to modify the arrangement of elements in an array.

Python
import array
my_array = array.array('i', [1, 2, 3, 4, 5])
print("Original array:", *my_array)
my_array.reverse()
print("Reversed array:", *my_array)

Output
Original array: 1 2 3 4 5
Reversed array: 5 4 3 2 1

Complexities for reversing elements in the Arrays:

Time Complexity: O(n)
Auxiliary Space: O(1)

Extend Element from Array

In Python, an array is used to store multiple values or elements of the same datatype in a single variable. The extend() function is simply used to attach an item from iterable to the end of the array. In simpler terms, this method is used to add an array of values to the end of a given or existing array.

Syntax of list extend()

The syntax of the extend() method:

list.extend(iterable)

Here, all the element of iterable are added to the end of list1

Example 1:

The provided code demonstrates the capability of extending an array to include additional elements. It imports the array module using an alias, creates an array of integers, prints the array before extension, extends the array using the extend() method, and finally prints the extended array. This concisely illustrates the ability to add elements to an existing array structure

Python
import array as arr 
a = arr.array('i', [1, 2, 3,4,5])
print("The before array extend  : ", end =" ")
for i in range (0, 5): 
  
    print (a[i], end =" ") 
    
print()
a.extend([6,7,8,9,10])
print("\nThe array after extend :",end=" ")

for i in range(0,10):  
  
    print(a[i],end=" ") 
    
print()

Output
The before array extend  :  1 2 3 4 5 

The array after extend : 1 2 3 4 5 6 7 8 9 10 

Example 2:

The provided code demonstrates the capacity to extend arrays with various data types, including integers and floats. It utilizes the array module, creates arrays of both data types, and extends them using the extend() method. The arrays are then printed before and after extension to illustrate the changes. This effectively showcases the ability to append elements to arrays of different data representations.

Python
import array as arr
a=arr.array('i',[1,2,3,4,5,6])
print("The Before extend array is :",end=" ")
for i in range(0,6):
  
    print(a[i],end=" ")
    
print()
a.extend([7,8,9,10,11,12])
print("\nThe After extend array is :",end=" ")

for i in range(0,12):
  
    print(a[i],end=" ")

print()
b = arr.array('d', [2.1,2.2,2.3,2.4,2.5,2.6])

print("\nThe before extend array is :",end=" ")

for i in range(0,6):
  
  print(b[i],end=" ")
  
print() 
b.extend([2.6,2.7,2.8,2.9])

print("\nThe after extend array is :",end=" ")

for i in range(0,9+1):
  
  print(b[i],end=" ")
  
print()  

Output
The Before extend array is : 1 2 3 4 5 6 

The After extend array is : 1 2 3 4 5 6 7 8 9 10 11 12 

The before extend array is : 2.1 2.2 2.3 2.4 2.5 2.6 

The after extend array is : 2.1 2.2 2.3 2.4 2...

Complexities for extend element from array:

Time Complexity : O(1)
Auxiliary Space: O(1)

Python Array – FAQs

What are Python Arrays?

Python Arrays are a data structure that can hold a collection of items. Unlike lists, which can hold items of different data types, arrays in Python are typically used to store items of the same data type. This makes them more efficient for numerical computations. Python does not have a built-in array data type, but the ‘array’ module provides an array type that can be used for this purpose.

How do you create an Array in Python?

To create an array in Python, you can use the ‘array’ module. First, you need to import the module, and then you can create an array by specifying the type code and the initial values. For example: “`python import array arr = array.array(‘i’, [1, 2, 3, 4]) # ‘i’ indicates an array of integers “` This creates an array of integers with the values 1, 2, 3, and 4.

What is the difference between Lists and Arrays in Python?

The main difference between lists and arrays in Python is that lists can hold items of different data types, while arrays are designed to hold items of the same data type. Arrays are more memory efficient and provide better performance for numerical operations. However, lists are more flexible and easier to use for general-purpose programming.

How do you access elements in a Python Array?

You can access elements in a Python array using indexing, similar to how you would access elements in a list. The index starts at 0 for the first element. For example: “`python import array arr = array.array(‘i’, [1, 2, 3, 4]) element = arr[2] # Accessing the third element print(element) # Output: 3 “`

Can you resize a Python Array?

Python arrays do not support dynamic resizing like lists do. However, you can create a new array with a different size and copy the elements from the old array to the new one. Alternatively, you can use the ‘array’ module’s ‘extend’ method to add more elements to the existing array, but this does not change the size of the array itself.




Previous Article
Next Article

Similar Reads

Benefit of NumPy arrays over Python arrays
The need for NumPy arises when we are working with multi-dimensional arrays. The traditional array module does not support multi-dimensional arrays. Let's first try to create a single-dimensional array (i.e one row & multiple columns) in Python without installing NumPy Package to get a more clear picture. C/C++ Code from array import * arr = ar
1 min read
Merge two sorted arrays in Python using heapq
Given two sorted arrays, the task is to merge them in a sorted manner. Examples: Input : arr1 = [1, 3, 4, 5] arr2 = [2, 4, 6, 8] Output : arr3 = [1, 2, 3, 4, 4, 5, 6, 8] Input : arr1 = [5, 8, 9] arr2 = [4, 7, 8] Output : arr3 = [4, 5, 7, 8, 8, 9] This problem has existing solution please refer Merge two sorted arrays link. We will solve this proble
2 min read
Intersection of two arrays in Python ( Lambda expression and filter function )
Given two arrays, find their intersection. Examples: Input: arr1[] = [1, 3, 4, 5, 7] arr2[] = [2, 3, 5, 6] Output: Intersection : [3, 5] We have existing solution for this problem please refer Intersection of two arrays link. We will solve this problem quickly in python using Lambda expression and filter() function. Implementation: C/C++ Code # Fun
1 min read
Python List Comprehension to find pair with given sum from two arrays
Given two unsorted arrays of distinct elements, the task is to find all pairs from both arrays whose sum is equal to x. Examples: Input : arr1 = [-1, -2, 4, -6, 5, 7] arr2 = [6, 3, 4, 0] x = 8 Output : [(5, 3), (4, 4)] Input : arr1 = [1, 2, 4, 5, 7] arr2 = [5, 6, 3, 4, 8] x = 9 Output : [(1, 8), (4, 5), (5, 4)] We have existing solution for this pr
2 min read
Python | Extension function operating on Arrays
Let's write a C extension function that can operate on contiguous arrays of data, as might be created by the array module or libraries like NumPy and this function should be general purpose and not specific to any one array library. The code should use Buffer Protocol to receive and process arrays in a portable manner. The code below is a C extensi
2 min read
Python - Bray-Curtis distance between two 1-D arrays
scipy.stats.braycurtis(array, axis=0) function calculates the Bray-Curtis distance between two 1-D arrays. Parameters : array: Input array or object having the elements to calculate the distance between each pair of the two collections of inputs. axis: Axis along which to be computed. By default axis = 0 Returns : distance between each pair of the
1 min read
Python: Operations on Numpy Arrays
NumPy is a Python package which means 'Numerical Python'. It is the library for logical computing, which contains a powerful n-dimensional array object, gives tools to integrate C, C++ and so on. It is likewise helpful in linear based math, arbitrary number capacity and so on. NumPy exhibits can likewise be utilized as an effective multi-dimensiona
3 min read
Python Lists VS Numpy Arrays
Here, we will understand the difference between Python List and Python Numpy array. What is a Numpy array?NumPy is the fundamental package for scientific computing in Python. Numpy arrays facilitate advanced mathematical and other types of operations on large numbers of data. Typically, such operations are executed more efficiently and with less co
7 min read
Python | Convert Numpy Arrays to Tuples
Given a numpy array, write a program to convert numpy array into tuples. Examples - Input: ([[1, 0, 0, 1, 0], [1, 2, 0, 0, 1]]) Output: ((1, 0, 0, 1, 0), (1, 2, 0, 0, 1)) Input: ([['manjeet', 'akshat'], ['nikhil', 'akash']]) Output: (('manjeet', 'akshat'), ('nikhil', 'akash')) Method #1: Using tuple and map Step-by-step approach : Import the NumPy
2 min read
Indexing Multi-dimensional arrays in Python using NumPy
In this article, we will cover the Indexing of Multi-dimensional arrays in Python using NumPy. NumPy is a general-purpose array-processing package. It provides a high-performance multidimensional array object and tools for working with these arrays. It is the fundamental package for scientific computing with Python. It contains various features.Not
3 min read
Accessing Data Along Multiple Dimensions Arrays in Python Numpy
NumPy (Numerical Python) is a Python library that comprises of multidimensional arrays and numerous functions to perform various mathematical and logical operations on them. NumPy also consists of various functions to perform linear algebra operations and generate random numbers. NumPy is often used along with packages like SciPy and Matplotlib for
3 min read
Convert Python Nested Lists to Multidimensional NumPy Arrays
Prerequisite: Python List, Numpy ndarray Both lists and NumPy arrays are inter-convertible. Since NumPy is a fast (High-performance) Python library for performing mathematical operations so it is preferred to work on NumPy arrays rather than nested lists. Method 1: Using numpy.array(). Approach : Import numpy package.Initialize the nested list and
2 min read
Compute the inner product of vectors for 1-D arrays using NumPy in Python
Python has a popular package called NumPy which used to perform complex calculations on 1-D and multi-dimensional arrays. To find the inner product of two arrays, we can use the inner() function of the NumPy package. Syntax: numpy.inner(array1, array2) Parameters: array1, array2: arrays to be evaluated Returns: Inner Product of two arrays Example 1
2 min read
Compute the inner product of vectors for 1-D arrays using NumPy in Python
Python has a popular package called NumPy which used to perform complex calculations on 1-D and multi-dimensional arrays. To find the inner product of two arrays, we can use the inner() function of the NumPy package. Syntax: numpy.inner(array1, array2) Parameters: array1, array2: arrays to be evaluated Returns: Inner Product of two arrays Example 1
2 min read
How to convert 1-D arrays as columns into a 2-D array in Python?
Let's see a program to convert 1-D arrays as columns into a 2-D array using NumPy library in Python. So, for solving this we are using numpy.column_stack() function of NumPy. This function takes a sequence of 1-D arrays and stack them as columns to make a single 2-D array. Syntax : numpy.column_stack(tuple) Parameters : tup : [sequence of ndarrays]
1 min read
How to rank Python NumPy arrays with ties?
In this article, we are going to see how to rank Numpy arrays with ties-breakers in Python. The ranking is an essential statistical operation used in numerous fields like data science, sociology, etc. A very brute-force approach would be to sort the indices of the array in order of their corresponding values. Such an approach would be handy in case
3 min read
intersection_update() in Python to find common elements in n arrays
We are given list of n number of arrays, find all common elements in given arrays ? Examples: Input : arr = [[1,2,3,4], [8,7,3,2], [9,2,6,3], [5,1,2,3]] Output : Common Elements = [2,3] We can solve this problem quickly in python using intersection_update() method of Set() data structure. How intersection_update() works ? Suppose we have two sets A
1 min read
Set update() in Python to do union of n arrays
We are given n arrays of any size which may have common elements, we need to combine all these arrays in such a way that each element should occurs only once and elements should be in sorted order? Examples: Input : arr = [[1, 2, 2, 4, 3, 6], [5, 1, 3, 4], [9, 5, 7, 1], [2, 4, 1, 3]] Output : [1, 2, 3, 4, 5, 6, 7, 9] A simple solution for this prob
2 min read
Different Ways to Create Numpy Arrays in Python
Creating NumPy arrays is a fundamental aspect of working with numerical data in Python. NumPy provides various methods to create arrays efficiently, catering to different needs and scenarios. In this article, we will see how we can create NumPy arrays using different ways and methods. Ways to Create Numpy ArraysBelow are some of the ways by which w
3 min read
Find Common Elements in Two Arrays in Python
Given two arrays arr1[] and arr2[], the task is to find all the common elements among them. Examples: Input: arr1[] = {1, 2, 3, 4, 5}, arr2[] = {3, 4, 5, 6, 7}Output: 3 4 5Explanation: 3, 4 and 5 are common to both the arrays. Input: arr1: {2, 4, 0, 5, 8}, arr2: {0, 1, 2, 3, 4}Output: 0 2 4Explanation: 0, 2 and 4 are common to both the arrays. Find
8 min read
Python | Using 2D arrays/lists the right way
Python provides powerful data structures called lists, which can store and manipulate collections of elements. Also provides many ways to create 2-dimensional lists/arrays. However one must know the differences between these ways because they can create complications in code that can be very difficult to trace out. In this article, we will explore
9 min read
Python slicing multi-dimensional arrays
Python's NumPy package makes slicing multi-dimensional arrays a valuable tool for data manipulation and analysis. It enables efficient subset data extraction and manipulation from arrays, making it a useful skill for any programmer, engineer, or data scientist. Python Slicing Multi-Dimensional ArraysSlicing is a method for taking out an array secti
4 min read
Convert Python List to numpy Arrays
A list in Python is a linear data structure that can hold heterogeneous elements that do not require to be declared and are flexible to shrink and grow. On the other hand, an array is a data structure that can hold homogeneous elements. Arrays are implemented in Python using the NumPy library. Arrays require less memory than lists. The similarity b
5 min read
Find common elements in three sorted arrays by dictionary intersection
One way to efficiently find shared items in three sorted arrays is by using dictionary intersection. However, it's important to note that dictionaries are commonly used for unique keys, so if there are duplicate elements in your arrays, some adjustments may be needed to make this approach work. Given three arrays sorted in non-decreasing order, pri
4 min read
Given two arrays, find n+m-1 unique sum pairs
Given 2 integer arrays A and B(with no repeated elements in each) of size N and M respectively, form N + M - 1 pairs, each pair having one element from both the arrays, such that the sum of all the pairs is unique. For each pair, print the index of the elements in their respective array. Example: Input: N = 5, M = 3 A = [6, 5, 7, 1, 3] B = [2, 4, 8
3 min read
Find common values between two NumPy arrays
In NumPy, we can find common values between two arrays with the help intersect1d(). It will take parameter two arrays and it will return an array in which all the common elements will appear. Syntax: numpy.intersect1d(array1,array2) Parameter :Two arrays. Return :An array in which all the common element will appear. Example 1: Python Code import nu
1 min read
Find common values between two NumPy arrays
In this article, we are going to discuss how to find out the common values between 2 arrays. To find the common values, we can use the numpy.intersect1d(), which will do the intersection operation and return the common values between the 2 arrays in sorted order. Syntax: numpy.intersect1d(arr1, arr2, assume_unique = False, return_indices = False) P
2 min read
How to Concatenate two 2-dimensional NumPy Arrays?
Sometimes it might be useful or required to concatenate or merge two or more of these NumPy arrays. In this article, we will discuss various methods of concatenating two 2D arrays. But first, we have to import the NumPy package to use it: # import numpy package import numpy as np Then two 2D arrays have to be created to perform the operations, by u
5 min read
Compute the covariance matrix of two given NumPy arrays
In NumPy for computing the covariance matrix of two given arrays with help of numpy.cov(). In this, we will pass the two arrays and it will return the covariance matrix of two given arrays. Syntax: numpy.cov(m, y=None, rowvar=True, bias=False, ddof=None, fweights=None, aweights=None) Example 1: Python Code import numpy as np array1 = np.array([0, 1
2 min read
Calculate average values of two given NumPy arrays
Finding average of NumPy arrays is quite similar to finding average of given numbers. We just have to get the sum of corresponding array elements and then divide that sum with the total number of arrays. Let's see an example: Example 1: Calculate average values of two given NumPy 1d-arrays C/C++ Code # import library import numpy as np # create a n
1 min read
Article Tags :
Practice Tags :