{"id":36944,"date":"2026-10-03T20:08:32","date_gmt":"2026-10-03T20:08:32","guid":{"rendered":"https:\/\/www.askpython.com\/?p=36944"},"modified":"2026-10-04T17:39:44","modified_gmt":"2026-10-04T17:39:44","slug":"numpy-zeros_like","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python-modules\/numpy\/numpy-zeros_like","title":{"rendered":"NumPy zeros_like: Create a Zero Array from a Template"},"content":{"rendered":"<!-- article-type: how-to-use -->\n<!-- receipt-backed-first-person -->\n\n<p class=\"wp-block-paragraph\">When the next calculation needs an array of zeros that lines up with an array you already have, NumPy can build it for you. <code>np.zeros_like<\/code> creates that array, copying the shape and data type of the one you give it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I&#8217;ll show you how to override the dtype or shape, and when plain <code>np.zeros<\/code> fits better.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is NumPy zeros_like?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">NumPy\u2019s zeros_like function creates a zero-filled array from an existing array, carrying over its shape and data type unless you override either property.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">That makes zeros_like useful when the next calculation needs a zero array aligned with an array you already have. NumPy\u2019s zeros function is the alternative when you want to specify the dimensions directly.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Default from the input<\/th><th>Meaning<\/th><\/tr><\/thead><tbody><tr><td>shape<\/td><td>The output dimensions match the template.<\/td><\/tr><tr><td>dtype<\/td><td>The output uses the template\u2019s element type.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Prerequisites<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The examples assume Python, NumPy, and basic array creation.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Import NumPy with the conventional np alias.<\/li>\n\n<li>Use zeros_like when an existing array should define the output shape and default data type.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Step 1: Create zeros from an existing array<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Use the input array as the template when each zero should line up with one existing element. This example starts with a 2-by-3 integer array, then prints both arrays so you can compare their dimensions.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n\nvalues = np.arange(6).reshape(2, 3)\nzeros = np.zeros_like(values)\n\nprint(&quot;template:\\n&quot;, values)\nprint(&quot;zeros:\\n&quot;, zeros)\nprint(&quot;shape and dtype:&quot;, zeros.shape, zeros.dtype)\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">The result has shape (2, 3) and dtype int64, inherited from values. I ran this example with NumPy 2.4.6, and the printed zero array kept both rows and all three columns.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1920\" height=\"1040\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2026\/10\/numpy-zeros-like-output.png\" alt=\"NumPy zeros_like output for a 2 by 3 array and dtype and shape overrides\" class=\"wp-image-71808\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2026\/10\/numpy-zeros-like-output.png 1920w, https:\/\/www.askpython.com\/wp-content\/uploads\/2026\/10\/numpy-zeros-like-output-300x163.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2026\/10\/numpy-zeros-like-output-1024x555.png 1024w, https:\/\/www.askpython.com\/wp-content\/uploads\/2026\/10\/numpy-zeros-like-output-768x416.png 768w, https:\/\/www.askpython.com\/wp-content\/uploads\/2026\/10\/numpy-zeros-like-output-1536x832.png 1536w\" sizes=\"auto, (max-width: 1920px) 100vw, 1920px\" \/><figcaption>A tested zeros_like call keeps the input shape and allows deliberate dtype and shape overrides.<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Step 2: Override the dtype or shape<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Pass dtype when the zero values need a different numeric type, or shape when the result needs different dimensions. Both options override the corresponding property that would otherwise come from the template.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Call<\/th><th>Effect<\/th><\/tr><\/thead><tbody><tr><td>np.zeros_like(values)<\/td><td>Uses values.shape and values.dtype<\/td><\/tr><tr><td>np.zeros_like(values, dtype=np.float64)<\/td><td>Keeps the shape and uses float64<\/td><\/tr><tr><td>np.zeros_like(values, shape=(3, 2))<\/td><td>Uses the supplied shape and the input dtype<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nfloat_zeros = np.zeros_like(values, dtype=np.float64)\nreshaped_zeros = np.zeros_like(values, shape=(3, 2))\n\nprint(float_zeros.dtype)\nprint(reshaped_zeros.shape)\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">The output is float64 for the first call and (3, 2) for the second. The shape override changes the dimensions, so it no longer matches the template even though the input still supplies the default dtype.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Choose zeros_like or zeros<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Choose zeros_like when an existing array should supply the shape and, unless overridden, the dtype. Choose zeros when you already know the dimensions and want to state them directly.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Template array available: use zeros_like(template).<\/li>\n\n<li>Dimensions known independently: use zeros(shape).<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">For example, zeros_like(values) follows the array\u2019s current dimensions, while zeros((2, 3)) spells out the requested dimensions. See the <a href=\"https:\/\/www.askpython.com\/python-modules\/numpy\/numpy-zeros\" type=\"post\" id=\"36933\">NumPy zeros guide<\/a> for the shape-based call.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Keep the template\u2019s metadata in view<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The input does more than provide a count of elements. Its shape and dtype set the defaults, so integer input produces integer zeros unless you pass another dtype.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">NumPy also accepts order to control memory layout, subok to choose whether to preserve an input subclass, and device for Array API interoperability. The <a href=\"https:\/\/numpy.org\/doc\/stable\/reference\/generated\/numpy.zeros_like.html\" target=\"_blank\" rel=\"noopener\">current NumPy reference<\/a> says device must be cpu when supplied, so do not treat this argument as general GPU placement.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ncheck = np.zeros_like(values)\nprint(check.shape == values.shape)\nprint(check.dtype == values.dtype)\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">Both checks print True for the default call. Keep the template when its metadata expresses the output you need, and pass only the overrides that should differ.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">NumPy zeros_like questions<\/h2>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1791055000001\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">What does NumPy zeros_like do?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>It creates an array filled with zeros, using an input array&#8217;s shape and data type by default.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1791055000002\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">How do I make zeros_like return floats?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Pass a floating-point dtype, such as dtype=np.float64, to override the input array&#8217;s data type.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1791055000003\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">How is zeros_like different from zeros?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>zeros_like takes an array template and inherits its shape and dtype by default. zeros takes the requested shape directly.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>When the next calculation needs an array of zeros that lines up with an array you already have, NumPy can build it for you. np.zeros_like creates that array, copying the shape and data type of the one you give it. I&#8217;ll show you how to override the dtype or shape, and when plain np.zeros fits [&hellip;]<\/p>\n","protected":false},"author":46,"featured_media":71810,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[93],"tags":[],"class_list":["post-36944","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\/36944","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\/46"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=36944"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/36944\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/71810"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=36944"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=36944"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=36944"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}