Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] Python – Return NumPy Array From Function

#1
Python – Return NumPy Array From Function

5/5 – (1 vote)

Do you need to create a function that returns a NumPy array but you don’t know how? No worries, in sixty seconds, you’ll know! Go! ?

A Python function can return any object such as a NumPy Array. To return an array, first create the array object within the function body, assign it to a variable arr, and return it to the caller of the function using the keyword operation “return arr“.

? Recommended Tutorial: How to Initialize a NumPy Array? 6 Easy Ways

Create and Return 1D Array


For example, the following code creates a function create_array() of numbers 0, 1, 2, …, 9 using the np.arange() function and returns the array to the caller of the function:

import numpy as np def create_array(): ''' Function to return array ''' return np.arange(10) numbers = create_array()
print(numbers)
# [0 1 2 3 4 5 6 7 8 9]

The np.arange([start,] stop[, step]) function creates a new NumPy array with evenly-spaced integers between start (inclusive) and stop (exclusive).

The step size defines the difference between subsequent values. For example, np.arange(1, 6, 2) creates the NumPy array [1, 3, 5].

To better understand the function, have a look at this video:

YouTube Video

I also created this figure to demonstrate how NumPy’s arange() function works on three examples:


In the code example, we used np.arange(10) with default start=0 and step=1 only specifying the stop=10 argument.

If you need an even deeper understanding, I’d recommend you check out our full guide on the Finxter blog.

? Recommended Tutorial: NumPy Arange Function — A Helpful Illustrated Guide

Create and Return 2D NumPy Array


You can also create a 2D (or multi-dimensional) array in a Python function by first creating a 2D or (xD) nested list and converting the nested list to a NumPy array by passing it into the np.array() function.

The following code snippet uses nested list comprehension to create a 2D NumPy array following a more complicated creation pattern:

import numpy as np def create_array(a,b): ''' Function to return array ''' lst = [[(i+j)**2 for i in range(a)] for j in range(b)] return np.array(lst) arr = create_array(4,3)
print(arr)

Output:

[[ 0 1 4 9] [ 1 4 9 16] [ 4 9 16 25]]

I definitely recommend reading the following tutorial to understand nested list comprehension in Python:

? Recommended Tutorial: Nested List Comprehension in Python

More Ways


There are many other ways to return an array in Python.

For example, you can use either of those methods inside the function body to create and initialize a NumPy array:

To get a quick overview what to put into the function and how these methods work, I’d recommend you check out our full tutorial.

? Recommended Tutorial: How to Initialize a NumPy Array? 6 Easy Ways

Related Tutorials


Programmer Humor


Q: How do you tell an introverted computer scientist from an extroverted computer scientist? A: An extroverted computer scientist looks at your shoes when he talks to you.


https://www.sickgaming.net/blog/2022/10/...-function/
Reply



Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tut] Python Async Function xSicKxBot 0 1,271 05-08-2023, 04:28 AM
Last Post: xSicKxBot
  [Tut] Python Library Hijacking – A Simple Demonstration on NumPy xSicKxBot 0 1,247 11-22-2022, 01:48 AM
Last Post: xSicKxBot
  [Tut] Solidity Bytes and String Arrays, Concat, Allocating Memory, and Array Literals xSicKxBot 0 1,199 10-25-2022, 09:13 AM
Last Post: xSicKxBot
  [Tut] How to Convert Pandas DataFrame/Series to NumPy Array? xSicKxBot 0 1,213 10-24-2022, 02:13 PM
Last Post: xSicKxBot
  [Tut] What’s the Difference Between return and break in Python? xSicKxBot 0 1,297 10-23-2022, 01:46 AM
Last Post: xSicKxBot
  [Tut] How to Return a File From a Function in Python? xSicKxBot 0 1,325 10-21-2022, 09:47 AM
Last Post: xSicKxBot
  [Tut] How to Print a NumPy Array Without Scientific Notation in Python xSicKxBot 0 1,216 10-20-2022, 11:44 AM
Last Post: xSicKxBot
  [Tut] Python Return String From Function xSicKxBot 0 1,198 10-14-2022, 01:13 PM
Last Post: xSicKxBot
  [Tut] How to Find the Longest String in a NumPy Array? xSicKxBot 0 1,099 09-19-2022, 11:34 AM
Last Post: xSicKxBot
  [Tut] Combine Images Using Numpy xSicKxBot 0 1,217 09-18-2022, 10:23 AM
Last Post: xSicKxBot

Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016