{"id":128916,"date":"2022-10-15T05:42:58","date_gmt":"2022-10-15T05:42:58","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=791161"},"modified":"2022-10-15T05:42:58","modified_gmt":"2022-10-15T05:42:58","slug":"python-return-numpy-array-from-function","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2022\/10\/15\/python-return-numpy-array-from-function\/","title":{"rendered":"Python \u2013 Return NumPy Array From Function"},"content":{"rendered":"\n<div class=\"kk-star-ratings kksr-auto kksr-align-left kksr-valign-top\" data-payload=\"{&quot;align&quot;:&quot;left&quot;,&quot;id&quot;:&quot;791161&quot;,&quot;slug&quot;:&quot;default&quot;,&quot;valign&quot;:&quot;top&quot;,&quot;ignore&quot;:&quot;&quot;,&quot;reference&quot;:&quot;auto&quot;,&quot;class&quot;:&quot;&quot;,&quot;count&quot;:&quot;1&quot;,&quot;readonly&quot;:&quot;&quot;,&quot;score&quot;:&quot;5&quot;,&quot;best&quot;:&quot;5&quot;,&quot;gap&quot;:&quot;5&quot;,&quot;greet&quot;:&quot;Rate this post&quot;,&quot;legend&quot;:&quot;5\\\/5 - (1 vote)&quot;,&quot;size&quot;:&quot;24&quot;,&quot;width&quot;:&quot;142.5&quot;,&quot;_legend&quot;:&quot;{score}\\\/{best} - ({count} {votes})&quot;,&quot;font_factor&quot;:&quot;1.25&quot;}\">\n<div class=\"kksr-stars\">\n<div class=\"kksr-stars-inactive\">\n<div class=\"kksr-star\" data-star=\"1\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"2\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"3\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"4\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"5\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div class=\"kksr-stars-active\" style=\"width: 142.5px;\">\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/div>\n<div class=\"kksr-legend\" style=\"font-size: 19.2px;\"> 5\/5 &#8211; (1 vote) <\/div>\n<\/div>\n<p>Do you need to <strong>create a function that returns a NumPy array<\/strong> but you don&#8217;t know how? No worries, in sixty seconds, you&#8217;ll know! Go! <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f680.png\" alt=\"\ud83d\ude80\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/><\/p>\n<p class=\"has-pale-cyan-blue-background-color has-background\">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 <code>arr<\/code>, and return it to the caller of the function using the <a title=\"Return Keyword in Python \u2013 A Simple Illustrated Guide\" rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-return\/\" target=\"_blank\">keyword <\/a>operation &#8220;<code>return arr<\/code>&#8220;. <\/p>\n<p class=\"has-base-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f449.png\" alt=\"\ud83d\udc49\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/> <strong>Recommended Tutorial<\/strong>: <a href=\"https:\/\/blog.finxter.com\/how-to-initialize-a-numpy-array-6-easy-ways\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/how-to-initialize-a-numpy-array-6-easy-ways\/\" target=\"_blank\" rel=\"noreferrer noopener\">How to Initialize a NumPy Array? 6 Easy Ways<\/a><\/p>\n<h2>Create and Return 1D Array<\/h2>\n<p>For example, the following code creates a function <code>create_array()<\/code> of numbers 0, 1, 2, &#8230;, 9 using the <code><a href=\"https:\/\/blog.finxter.com\/numpy-arange\/\" data-type=\"post\" data-id=\"548\" target=\"_blank\" rel=\"noreferrer noopener\">np.arange()<\/a><\/code> function and returns the array to the caller of the function:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"6\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import numpy as np def create_array(): ''' Function to return array ''' return np.arange(10) numbers = create_array()\nprint(numbers)\n# [0 1 2 3 4 5 6 7 8 9]\n<\/pre>\n<p>The <code>np.arange([start,] stop[, step])<\/code> function creates a new NumPy array with evenly-spaced integers between <code>start<\/code> (inclusive) and <code>stop<\/code> (exclusive). <\/p>\n<p>The <code>step<\/code> size defines the difference between subsequent values. For example, <code>np.arange(1, 6, 2)<\/code> creates the NumPy array <code>[1, 3, 5]<\/code>.<\/p>\n<p>To better understand the function, have a look at this video:<\/p>\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube\"><a href=\"https:\/\/blog.finxter.com\/python-return-numpy-array-from-function\/\"><img decoding=\"async\" src=\"https:\/\/blog.finxter.com\/wp-content\/plugins\/wp-youtube-lyte\/lyteCache.php?origThumbUrl=https%3A%2F%2Fi.ytimg.com%2Fvi%2FVERAXaqUsjc%2Fhqdefault.jpg\" alt=\"YouTube Video\"><\/a><figcaption><\/figcaption><\/figure>\n<p>I also created this figure to demonstrate how NumPy&#8217;s <code>arange()<\/code> function works on three examples:<\/p>\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"576\" src=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/10\/image-134.png\" alt=\"\" class=\"wp-image-791189\" srcset=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/10\/image-134.png 1024w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/10\/image-134-300x169.png 300w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/10\/image-134-768x432.png 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<p>In the code example, we used <code>np.arange(10)<\/code> with default <code>start=0<\/code> and <code>step=1<\/code> only specifying the <code>stop=10<\/code> argument.<\/p>\n<p>If you need an even deeper understanding, I&#8217;d recommend you check out our full guide on the Finxter blog.<\/p>\n<p class=\"has-base-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f449.png\" alt=\"\ud83d\udc49\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/> <strong>Recommended Tutorial<\/strong>: <a href=\"https:\/\/blog.finxter.com\/numpy-arange\/\" data-type=\"URL\" data-id=\"https:\/\/blog.finxter.com\/numpy-arange\/\" target=\"_blank\" rel=\"noreferrer noopener\">NumPy Arange Function &#8212; A Helpful Illustrated Guide<\/a><\/p>\n<h2>Create and Return 2D NumPy Array<\/h2>\n<p class=\"has-base-background-color has-background\">You can also create a 2D (or multi-dimensional) array in a Python function by first creating a 2D or (xD) <a href=\"https:\/\/blog.finxter.com\/python-list-of-lists\/\" data-type=\"post\" data-id=\"7890\" target=\"_blank\" rel=\"noreferrer noopener\">nested list<\/a> and converting the nested list to a NumPy array by passing it into the <code><a href=\"https:\/\/blog.finxter.com\/numpy-tutorial\/\" data-type=\"post\" data-id=\"1356\" target=\"_blank\" rel=\"noreferrer noopener\">np.array()<\/a><\/code> function.<\/p>\n<p>The following code snippet uses <a href=\"https:\/\/blog.finxter.com\/how-does-nested-list-comprehension-work-in-python\/\" data-type=\"post\" data-id=\"7596\" target=\"_blank\" rel=\"noreferrer noopener\">nested list comprehension<\/a> to create a 2D NumPy array following a more complicated creation pattern:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">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)\nprint(arr)\n<\/pre>\n<p>Output:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">[[ 0 1 4 9] [ 1 4 9 16] [ 4 9 16 25]]<\/pre>\n<p>I definitely recommend reading the following tutorial to understand nested list comprehension in Python:<\/p>\n<p class=\"has-base-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f449.png\" alt=\"\ud83d\udc49\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/> <strong>Recommended Tutorial<\/strong>: <a href=\"https:\/\/blog.finxter.com\/how-does-nested-list-comprehension-work-in-python\/\" data-type=\"post\" data-id=\"7596\" target=\"_blank\" rel=\"noreferrer noopener\">Nested List Comprehension in Python<\/a><\/p>\n<\/p>\n<h2>More Ways<\/h2>\n<p>There are many other ways to return an array in Python. <\/p>\n<p>For example, you can use either of those methods inside the function body to create and initialize a NumPy array:<\/p>\n<ul>\n<li><strong>Method 1<\/strong>: Use <a rel=\"noreferrer noopener\" href=\"https:\/\/numpy.org\/doc\/stable\/reference\/generated\/numpy.array.html?highlight=numpy%20array#numpy.array\" target=\"_blank\"><code>np.array()<\/code><\/a><\/li>\n<li><strong>Method 2<\/strong>: Use <a rel=\"noreferrer noopener\" href=\"https:\/\/numpy.org\/doc\/stable\/reference\/generated\/numpy.zeros.html\" target=\"_blank\"><code>np.zeros()<\/code><\/a><\/li>\n<li><strong>Method 3<\/strong>: Use <a rel=\"noreferrer noopener\" href=\"https:\/\/numpy.org\/doc\/stable\/reference\/generated\/numpy.ones.html?highlight=ones#numpy.ones\" target=\"_blank\"><code>np.ones()<\/code><\/a><\/li>\n<li><strong>Method 4<\/strong>: Use <a rel=\"noreferrer noopener\" href=\"https:\/\/numpy.org\/doc\/stable\/reference\/generated\/numpy.full.html?highlight=full\" target=\"_blank\"><code>np.full()<\/code><\/a><\/li>\n<li><strong>Method 5<\/strong>: Use <a rel=\"noreferrer noopener\" href=\"https:\/\/numpy.org\/doc\/stable\/reference\/generated\/numpy.empty.html?highlight=empty#numpy.empty\" target=\"_blank\"><code>np.empty()<\/code><\/a><\/li>\n<li><strong>Method 6<\/strong>: Use <a rel=\"noreferrer noopener\" href=\"https:\/\/numpy.org\/doc\/stable\/reference\/generated\/numpy.arange.html?highlight=arange#numpy.arange\" target=\"_blank\"><code>np.arange()<\/code><\/a><\/li>\n<li><strong>Bonus<\/strong>: Initialize a <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/numpy-tutorial\/\" target=\"_blank\">NumPy<\/a> array with CSV data<\/li>\n<\/ul>\n<p>To get a quick overview what to put into the function and how these methods work, I&#8217;d recommend you check out our full tutorial.<\/p>\n<p class=\"has-base-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f449.png\" alt=\"\ud83d\udc49\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/> <strong>Recommended Tutorial<\/strong>: <a href=\"https:\/\/blog.finxter.com\/how-to-initialize-a-numpy-array-6-easy-ways\/\" data-type=\"post\" data-id=\"453551\" target=\"_blank\" rel=\"noreferrer noopener\">How to Initialize a NumPy Array? 6 Easy Ways<\/a><\/p>\n<h2>Related Tutorials<\/h2>\n<ul>\n<li><a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-return-string-from-function\/\" data-type=\"post\" data-id=\"784920\" target=\"_blank\">Python Return String From Function<\/a><\/li>\n<li><a href=\"https:\/\/blog.finxter.com\/python-return-dictionary-from-function\/\" data-type=\"post\" data-id=\"34362\" target=\"_blank\" rel=\"noreferrer noopener\">Python Return Dict From Function<\/a><\/li>\n<li><a href=\"https:\/\/blog.finxter.com\/python-return-set-from-function\/\" data-type=\"post\" data-id=\"34346\" target=\"_blank\" rel=\"noreferrer noopener\">Python Return Set From Function<\/a><\/li>\n<\/ul>\n<h2>Programmer Humor<\/h2>\n<pre class=\"wp-block-preformatted has-global-color-8-background-color has-background\"><code><strong>Q<\/strong>: How do you tell an introverted computer scientist from an extroverted computer scientist? <strong>A<\/strong>: An extroverted computer scientist looks at <strong><em>your<\/em><\/strong> shoes when he talks to you.<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>5\/5 &#8211; (1 vote) Do you need to create a function that returns a NumPy array but you don&#8217;t know how? No worries, in sixty seconds, you&#8217;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 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[857],"tags":[73,468,528],"class_list":["post-128916","post","type-post","status-publish","format-standard","hentry","category-python-tut","tag-programming","tag-python","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/128916","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/comments?post=128916"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/128916\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=128916"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=128916"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=128916"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}