{"id":7229,"date":"2020-07-24T16:06:11","date_gmt":"2020-07-24T16:06:11","guid":{"rendered":"https:\/\/www.askpython.com\/?p=7229"},"modified":"2020-07-24T16:06:13","modified_gmt":"2020-07-24T16:06:13","slug":"difference-between-python-list-vs-array","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/difference-between-python-list-vs-array","title":{"rendered":"Python List vs Array &#8211; 4 Differences to know!"},"content":{"rendered":"\n<p>Hey, folks! Hope you all are doing well. In this article, we will be focusing on the <strong>Difference between a Python List and Array in detail.<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>The main difference between a Python list and a Python array is that a list is part of the Python standard package whereas, for an array, the &#8220;array&#8221; module needs to be imported. Lists in Python replace the array data structure with a few exceptional cases.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1. How Lists and Arrays Store Data<\/h2>\n\n\n\n<p>As we all know, Data structures are used to store the data effectively. <\/p>\n\n\n\n<p>In this case, a <a href=\"https:\/\/www.askpython.com\/python\/list\/python-list\" class=\"rank-math-link\">list <\/a>can store heterogeneous data values into it. That is, data items of different data types can be accommodated into a Python List.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nlst = &#x5B;1,2,3,4,&#039;Python&#039;]\nprint(lst)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n&#x5B;1,2,3,4,&#039;Python&#039;]\n<\/pre><\/div>\n\n\n<p>On the other hand, <a href=\"https:\/\/www.askpython.com\/python\/array\/python-array-examples\" class=\"rank-math-link\">Arrays<\/a> store homogeneous elements into it i.e. they store elements that belong to the same type.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport array\n\narr = array.array(&#039;i&#039;, &#x5B;10, 20, 30, 40])\nprint(arr)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\narray(&#039;i&#039;, &#x5B;10, 20, 30, 40])\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">2. Declaration of Array vs. List<\/h2>\n\n\n\n<p>Python has got &#8220;List&#8221; as a built-in data structure. That is the reason, Lists do not need to be declared in Python.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nlst = &#x5B;1, 2, 3, 4]\n<\/pre><\/div>\n\n\n<p>On the other hand, Arrays in Python need to be declared. We can declare an array using the below techniques:<\/p>\n\n\n\n<p><strong>Array Module<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport array\narray-name = array.array(&#039;format-code&#039;, &#x5B;elements])\n<\/pre><\/div>\n\n\n<p><strong>NumPy Module<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy\narray-name = numpy.array(&#x5B;elements])\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">3. Superior Mathematical Operations with Arrays<\/h2>\n\n\n\n<p>Arrays provide an upper hand when it comes to performing Mathematical operations. The <a class=\"rank-math-link\" href=\"https:\/\/www.askpython.com\/python-modules\/numpy\/python-numpy-arrays\">NumPy module<\/a> provides us with the array structure to store data values and manipulate them easily.<\/p>\n\n\n\n<p><strong>Example with Arrays:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy\narr = numpy.array(&#x5B;1,2,3,4])\npr = arr*5\nprint(pr)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n&#x5B; 5 10 15 20]\n<\/pre><\/div>\n\n\n<p>Unlike lists, wherein the operations performed on the list do not reflect into the results as seen in the below example with list operations.<\/p>\n\n\n\n<p>Here, we have tried to multiply the constant value (5) with the list, which does not reflect anything in the output. Because Lists are not open to direct mathematical manipulations with any data values. <\/p>\n\n\n\n<p>So, if we want to multiply 5 with the elements of the list, we will have to individually multiply 5 with each element of the list.<\/p>\n\n\n\n<p><strong>Example with Lists:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nlst = &#x5B;1,2,3,4]\npr = lst*5\nprint(lst)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n&#x5B;1, 2, 3, 4]\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">4. Resizing the data structure<\/h2>\n\n\n\n<p>Python Lists being an inbuilt data structure can be resized very easily and efficiently. <\/p>\n\n\n\n<p>While on the other side, Arrays prove to have very poor performance in terms of resizing the memory of the array. Instead, we will have to copy the array into another one to scale and resize it.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>By this, we have come to the end of this topic. Feel free to comment below, in case you come across any question.<\/p>\n\n\n\n<p>Till then, Happy Learning!!<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">References<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/stackoverflow.com\/questions\/176011\/python-list-vs-array-when-to-use\" class=\"rank-math-link\" target=\"_blank\" rel=\"noopener\">Python Array vs List &#8212; StackOverflow<\/a><\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Hey, folks! Hope you all are doing well. In this article, we will be focusing on the Difference between a Python List and Array in detail. The main difference between a Python list and a Python array is that a list is part of the Python standard package whereas, for an array, the &#8220;array&#8221; module [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":7359,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-7229","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/7229","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\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=7229"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/7229\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/7359"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=7229"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=7229"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=7229"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}