Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

function
python
numpy
communitycreator

What is the numpy.concatenate() function in Python?

Onyejiaku Theophilus Chidalu

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

The concatenate() function in Python is used to concatenate or join a sequence of input arrays along an existing axis.

Syntax

numpy.concatenate((a1, a2, ...), axis=0, out=None, dtype=None, casting="same_kind")
Syntax for the numpy.concatenate() function

Parameter value

The concatenate() function takes the following parameter values.

  • (a1, a2, ...): These are the input arrays to be concatenated. This is a required parameter.
  • axis: This is the given axis along which the input arrays will be joined. This is an optional parameter.
  • out: This is the destination path for the result. This is an optional parameter.
  • dtype: This is the data type of the desired output array. The value can be inferred from the data type of the input arrays. This is an optional parameter.
  • casting: This controls the kind of datacasting that may occur. This is an optional parameter.

Return value

The concatenate() function returns the concatenated array.

Example

import numpy as np
# creating input arrays
a = np.array([[1, 2, 3], [4, 5, 6]])
b = np.array([[7, 8, 9]])
# concatenating the arrays
myarray = np.concatenate((a,b), axis=0)
# printing the concatenated array
print(myarray)
Implementing the numpy.concatenate() function

Explanation

  • Line 1: We import the numpy module.
  • Lines 3–4: We create input arrays, a and b, using the np.array() function.
  • Line 7: We concatenate the input arrays using the np.concatenate() function. The result is assigned to a variable, myarray.
  • Line 10: We print the concatenated array, myarray.

RELATED TAGS

function
python
numpy
communitycreator

CONTRIBUTOR

Onyejiaku Theophilus Chidalu

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring