{"id":38037,"date":"2026-10-03T19:12:34","date_gmt":"2026-10-03T19:12:34","guid":{"rendered":"https:\/\/www.askpython.com\/?p=38037"},"modified":"2026-10-04T17:39:46","modified_gmt":"2026-10-04T17:39:46","slug":"numpy-empty-and-empty_like","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python-modules\/numpy\/numpy-empty-and-empty_like","title":{"rendered":"NumPy empty and empty_like"},"content":{"rendered":"<!-- article-type: tutorial -->\n<!-- receipt-backed-first-person -->\n\n<p class=\"wp-block-paragraph\">When you&#8217;re about to fill a NumPy array with your own values straight away, you only need NumPy to set aside the space. <code>np.empty<\/code> creates that array without setting its values, and <code>np.empty_like<\/code> does the same using another array&#8217;s shape and data type.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here you&#8217;ll create and fill arrays with both functions, and see why you must fill them before reading.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What np.empty and np.empty_like return<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">NumPy\u2019s empty functions allocate an array without setting its numeric entries to useful values. \u201cEmpty\u201d here means uninitialized storage, not an array with zero elements.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Use np.empty(shape) when you choose the dimensions yourself. Use np.empty_like(prototype) when the new array should inherit a prototype\u2019s shape and data type.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">With np.empty, shape is required and dtype defaults to float64. Its order option controls row-major or column-major layout. np.empty_like inherits shape and dtype from its prototype, and its order option can override the memory layout.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Function<\/th><th>Shape and dtype<\/th><th>Entries<\/th><\/tr><\/thead><tbody><tr><td>np.empty(shape)<\/td><td>Set by arguments, dtype defaults to float64<\/td><td>Uninitialized for numeric types<\/td><\/tr><tr><td>np.empty_like(prototype)<\/td><td>Copied from prototype unless overridden<\/td><td>Uninitialized for numeric types<\/td><\/tr><tr><td>np.zeros(shape) or np.zeros_like(prototype)<\/td><td>Chosen or copied<\/td><td>Every entry is zero<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">What you need before allocating an array<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Import NumPy under the name np before running the examples. In a shape of (2, 3), the dimensions request two rows and three columns, and dtype selects the representation for each element.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Step 1: Allocate a new array with np.empty<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Pass the intended dimensions to np.empty, then assign every element before you inspect or calculate with the array. The slice assignment below initializes the complete allocation, so the printed values do not depend on whatever occupied that memory before allocation.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n\nvalues = np.empty((2, 3), dtype=np.int64)\nvalues&#x5B;:] = np.arange(6).reshape(2, 3)\nprint(values)\nprint(values.shape, values.dtype)\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1920\" height=\"916\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2026\/10\/numpy-empty-output.png\" alt=\"NumPy empty array after every element is assigned\" class=\"wp-image-71804\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2026\/10\/numpy-empty-output.png 1920w, https:\/\/www.askpython.com\/wp-content\/uploads\/2026\/10\/numpy-empty-output-300x143.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2026\/10\/numpy-empty-output-1024x489.png 1024w, https:\/\/www.askpython.com\/wp-content\/uploads\/2026\/10\/numpy-empty-output-768x366.png 768w, https:\/\/www.askpython.com\/wp-content\/uploads\/2026\/10\/numpy-empty-output-1536x733.png 1536w\" sizes=\"auto, (max-width: 1920px) 100vw, 1920px\" \/><figcaption class=\"wp-element-caption\">The allocated two-by-three array is filled before its values are printed.<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">np.arange(6) supplies known integers for reshape(2, 3), and assigning through values[:] writes into the allocated array rather than replacing the variable with another object.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 2: Match a prototype with np.empty_like<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Give np.empty_like an array or array-like prototype when its shape and dtype are the starting point. You can override dtype when the new values need a different representation, but the allocation still needs filling before a read.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nprototype = np.array(&#x5B;&#x5B;1, 2, 3], &#x5B;4, 5, 6]], dtype=np.int16)\nresult = np.empty_like(prototype)\nresult&#x5B;:] = np.arange(prototype.size).reshape(prototype.shape)\nprint(result)\nprint(result.shape, result.dtype)\n\nas_float = np.empty_like(prototype, dtype=np.float64)\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">The prototype supplies default shape and dtype, not values, and the final call changes dtype alone while keeping the same dimensions.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Check the array after assigning its values<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The assignments made the printed values [[0, 1, 2], [3, 4, 5]] in both arrays. Their shapes were (2, 3), with dtypes int64 for np.empty and int16 for np.empty_like.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I filled both allocations before printing, so the output comes from my assignments rather than any values in the storage before initialization. I used an integer dtype for np.empty and inherited int16 from the prototype for np.empty_like, which makes the dtype choice visible without reading arbitrary contents.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Allocation<\/th><th>Printed shape<\/th><th>Printed dtype<\/th><\/tr><\/thead><tbody><tr><td>np.empty, after assignment<\/td><td>(2, 3)<\/td><td>int64<\/td><\/tr><tr><td>np.empty_like, after assignment<\/td><td>(2, 3)<\/td><td>int16<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">When an empty array has no elements<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A zero-element array is different from an uninitialized array. Check array.size == 0 when you need to know whether the element count is zero. An array returned by np.empty((2, 3)) has six elements even before they are initialized.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The shape determines the number of allocated entries. It does not decide whether those entries have been assigned meaningful values.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\npython3 -c &#039;import numpy as np; a = np.empty((0, 3)); print(a.size)&#039;\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1920\" height=\"358\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2026\/10\/numpy-zero-size.png\" alt=\"NumPy zero-element array size output in a terminal\" class=\"wp-image-71805\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2026\/10\/numpy-zero-size.png 1920w, https:\/\/www.askpython.com\/wp-content\/uploads\/2026\/10\/numpy-zero-size-300x56.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2026\/10\/numpy-zero-size-1024x191.png 1024w, https:\/\/www.askpython.com\/wp-content\/uploads\/2026\/10\/numpy-zero-size-768x143.png 768w, https:\/\/www.askpython.com\/wp-content\/uploads\/2026\/10\/numpy-zero-size-1536x286.png 1536w\" sizes=\"auto, (max-width: 1920px) 100vw, 1920px\" \/><figcaption class=\"wp-element-caption\">The zero dimension makes the array\u2019s size 0.<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Choose allocation from the values you need<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Use np.empty or np.empty_like only when your next operation will assign every entry before anything reads it. If zero is the intended starting value, choose <a href=\"https:\/\/www.askpython.com\/python-modules\/numpy\/numpy-zeros\">np.zeros<\/a> or <a href=\"https:\/\/www.askpython.com\/python-modules\/numpy\/numpy-zeros_like\">np.zeros_like<\/a> instead.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nnext_values = np.zeros_like(prototype)\nprint(next_values)\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">NumPy empty questions<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">These answers separate allocation from initialization and from a zero-element shape.<\/p>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1791053109803\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">Does np.empty create an array filled with zeros?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>No. Numeric entries are uninitialized and may contain arbitrary values. Assign every element before reading the array.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1791053109804\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">How do I create a NumPy array with no elements?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Pass a shape with a zero dimension, such as np.empty((0, 3)). Check array.size == 0 to test whether the array contains no elements.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1791053109805\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">What does np.empty_like copy?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>By default, it takes the prototype\u2019s shape and dtype. It does not copy the prototype\u2019s values, and the new numeric entries must be assigned before they are read.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>When you&#8217;re about to fill a NumPy array with your own values straight away, you only need NumPy to set aside the space. np.empty creates that array without setting its values, and np.empty_like does the same using another array&#8217;s shape and data type. Here you&#8217;ll create and fill arrays with both functions, and see why [&hellip;]<\/p>\n","protected":false},"author":41,"featured_media":71806,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[93],"tags":[],"class_list":["post-38037","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-numpy"],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/38037","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/41"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=38037"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/38037\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/71806"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=38037"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=38037"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=38037"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}