{"id":18630,"date":"2021-12-17T13:25:45","date_gmt":"2021-12-17T07:55:45","guid":{"rendered":"https:\/\/java2blog.com\/?p=18630"},"modified":"2021-12-17T15:09:30","modified_gmt":"2021-12-17T09:39:30","slug":"python-array-size","status":"publish","type":"post","link":"https:\/\/java2blog.com\/python-array-size\/","title":{"rendered":"Python array size: get size of array in Python"},"content":{"rendered":"<p>In this article, we will see how to get size of array in Python.<br \/>\n<div id=\"toc_container\" class=\"toc_light_blue no_bullets\"><p class=\"toc_title\">Table of Contents<\/p><ul class=\"toc_list\"><li><a href=\"#Python_array_size\">Python array size<\/a><\/li><li><a href=\"#Ways_to_calculate_the_size_of_array_in_Python\">Ways to calculate the size of array in Python<\/a><ul><li><a href=\"#Using_the_len_function\">Using the len() function<\/a><\/li><li><a href=\"#Using_the_numpysize_attribute\">Using the numpy.size() attribute<\/a><\/li><li><a href=\"#Using_the_numpyshape_attribute\">Using the numpy.shape() attribute<\/a><\/li><\/ul><\/li><li><a href=\"#Conclusion\">Conclusion<\/a><\/li><\/ul><\/div>\n\n<h2><span id=\"Python_array_size\">Python array size<\/span><\/h2>\n<p>An array stores a collection of similar elements in a contiguous memory location. In Python, we have a list, an iterable, and a <code>numpy<\/code> array to work as arrays. <\/p>\n<p>We can perform various operations on arrays. The array size tells how many elements are present in the array. We can use the size of the array to calculate how much memory the array occupies by multiplying the size of the array with the size of individual elements.<\/p>\n<h2><span id=\"Ways_to_calculate_the_size_of_array_in_Python\">Ways to calculate the size of array in Python<\/span><\/h2>\n<p>Let us now discuss the different methods available to calculate the size of array in Python.<\/p>\n<h3><span id=\"Using_the_len_function\">Using the <code>len()<\/code> function<\/span><\/h3>\n<p>The <code>len()<\/code> function returns the length of an iterable like a list, <code>numpy<\/code> array, or string. <\/p>\n<p>For example,<\/p>\n<pre code= \"python\">\nimport numpy as np\narr = np.array([8,5,6,5])\nlst = [1,2,4]\nprint(len(arr),len(lst))\n<\/pre>\n<p>Output:<\/p>\n<div class=\"content-box-green\">\n4 3\n<\/div>\n<p>In the above example, we initialized a <code>numpy<\/code> array and a list and returned their length using the <code>len()<\/code> function.<\/p>\n<p>There is a drawback to this method. If we have a multi-dimensional array, it will not return the size of the array. It will consider every array within the outer array to be a single element and return that number.<\/p>\n<p>We can understand this better with an example as shown below.<\/p>\n<pre code = \"python\">\nimport numpy as np\narr = np.array([[1,5,3],[1,5,8]])\nlst = [[1,2,5],[5,2]]\nprint(len(arr),len(lst))    \n<\/pre>\n<p>Output:<\/p>\n<div class=\"content-box-green\">\n2 2\n<\/div>\n<p>In the above example, we can see that the <code>len()<\/code> functions return the length as 2 and do not count the elements of the inner arrays. <\/p>\n<section class=\"read-more-posts\">\n<div class=\"rm-header\">\n<h2>Further reading:<\/h2>\n<\/div>\n<div class=\"rm-wrap\">\n<div class=\"rm-item\">\n<h5><a href=\"https:\/\/java2blog.com\/print-array-python\/\" target=\"_blank\" rel=\"noopener\">Print array in Python<\/a><\/h5>\n<div class=\"ex\">\n            <a href=\"https:\/\/java2blog.com\/print-array-python\/\" target=\"_blank\" rel=\"noopener\">Read more \u2192<\/a>\n        <\/div>\n<\/div>\n<div class=\"rm-item\">\n<h5><a href=\"https:\/\/java2blog.com\/initialize-array-python\/\" target=\"_blank\" rel=\"noopener\">Initialize array in Python<\/a><\/h5>\n<div class=\"ex\">\n            <a href=\"https:\/\/java2blog.com\/initialize-array-python\/\" target=\"_blank\" rel=\"noopener\">Read more \u2192<\/a>\n        <\/div>\n<\/p><\/div>\n<\/div>\n<\/section>\n<h3><span id=\"Using_the_numpysize_attribute\">Using the <code>numpy.size()<\/code> attribute<\/span><\/h3>\n<p>The <code>numpy<\/code> arrays have an attribute called <code>numpy.size()<\/code>, which can return the size of the given array. This function overcomes the drawback of multi-dimensional arrays in the <code>len()<\/code> function.<\/p>\n<p>See the code below.<\/p>\n<pre code = \"python\">\nimport numpy as np\narr = np.array([8,5,6,5])\nprint(arr.size)\narr1 = np.array([[1,5,3],[1,5,8]])\nprint(arr1.size)    \n<\/pre>\n<p>Output:<\/p>\n<div class=\"content-box-green\">\n4<br \/>\n6\n<\/div>\n<p>As you can see, the above code returns the size of a <code>numpy<\/code> array. This method is not compatible with lists. To make it work with lists, we can convert the list to a <code>numpy<\/code> array using the <code>numpy.array()<\/code> function.<\/p>\n<h3><span id=\"Using_the_numpyshape_attribute\">Using the <code>numpy.shape()<\/code> attribute<\/span><\/h3>\n<p>The <code>numpy.shape()<\/code> attribute returns the shape of the <code>numpy<\/code> array, which can be considered as the number of rows and columns of an array. We get the shape in a tuple. <\/p>\n<p>We can multiply the numbers in the tuple to get the size of the given array. This method also is limited to <code>numpy<\/code> arrays.<\/p>\n<p>We implement the above logic in the following example.<\/p>\n<pre code = \"python\">\nimport numpy as np\narr = np.array([8,5,6,5])\nprint(np.prod(arr.shape))\narr1 = np.array([[1,5,3],[1,5,8]])\nprint(np.prod(arr1.shape))\n<\/pre>\n<p>Output:<\/p>\n<div class=\"content-box-green\">\n4<br \/>\n6\n<\/div>\n<p>In the above example,<\/p>\n<ul>\n<li>The <code>shape()<\/code> attribute returns the shape of the given array.<\/li>\n<li>The <code>numpy.prod()<\/code> function multiplies all the elements to return the size of the array.<\/li>\n<\/ul>\n<h2><span id=\"Conclusion\">Conclusion<\/span><\/h2>\n<p>In this article, we discussed several ways to get the size of array in Python. The most simple method is using the <code>len()<\/code> function, but it has its drawbacks and does not work with multidimensional arrays. The <code>numpy.size()<\/code> and <code>numpy.shape()<\/code> methods overcome this array but lack compatibility with lists. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Table of ContentsPython array sizeWays to calculate the size of array in PythonUsing the len() functionUsing the numpy.size() attributeUsing the numpy.shape() attributeConclusion In this article, we will see how to get size of array in Python. Python array size An array stores a collection of similar elements in a contiguous memory location. In Python, we [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":18643,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"_mi_skip_tracking":false},"categories":[206],"tags":[],"_links":{"self":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/18630"}],"collection":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/comments?post=18630"}],"version-history":[{"count":0,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/18630\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/media\/18643"}],"wp:attachment":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/media?parent=18630"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/categories?post=18630"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/tags?post=18630"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}