{"id":674,"date":"2019-09-05T07:36:11","date_gmt":"2019-09-05T07:36:11","guid":{"rendered":"https:\/\/www.askpython.com\/?p=674"},"modified":"2023-02-16T19:57:21","modified_gmt":"2023-02-16T19:57:21","slug":"python-array-examples","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/array\/python-array-examples","title":{"rendered":"Python Array &#8211; 13 Examples"},"content":{"rendered":"\n<p>Python doesn&#8217;t have explicit array data structure. It&#8217;s because we can do the same things with the <a rel=\"noreferrer noopener\" aria-label=\"List (opens in a new tab)\" href=\"https:\/\/www.askpython.com\/python\/list\/python-list\" target=\"_blank\">List<\/a>.<\/p>\n\n\n\n<p>The list contains a collection of items and it supports add\/update\/delete\/search operations. That&#8217;s why there is not much use of a separate data structure in Python to support arrays. <\/p>\n\n\n\n<p>An array contains items of the same type but Python list allows elements of different types. This is the only feature wise difference between an array and a list. But it&#8217;s not a deal breaker and doesn&#8217;t warrant a new data structure support in Python.<\/p>\n\n\n\n<p>However, Python array module can be used to create an array like object for integer, float, and Unicode characters.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Python array Module<\/h2>\n\n\n\n<p>Python array module allows us to create an array with constraint on the data types. There are only a few data types supported by this module. <\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"624\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/09\/python-array-supported-type-code-1024x624.png\" alt=\"Python Array Supported Type Code\" class=\"wp-image-676\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/09\/python-array-supported-type-code-1024x624.png 1024w, https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/09\/python-array-supported-type-code-300x183.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/09\/python-array-supported-type-code-768x468.png 768w, https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/09\/python-array-supported-type-code.png 1470w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>Python Array Supported Type Code<\/figcaption><\/figure><\/div>\n\n\n\n<p>The Unicode type code has been deprecated in Python 3.3 and it will be removed in Python 4.0 release.<\/p>\n\n\n\n<p>So, we can create an array of integers and float using array module. Let&#8217;s get started with the array module and look at all the operations it provides.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">1. Creating an Array<\/h3>\n\n\n\n<p>The syntax to create an array is <em><code>array.array(typecode, values_list)<\/code><\/em>.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport array\n\n# creating array\nint_array = array.array(&#039;i&#039;, &#x5B;1, 2, 3, 4])\n\nfloat_array = array.array(&#039;f&#039;, &#x5B;1.1, 2.2, 3.3, 4.4])\n\n# unicode array support is deprecated and will be deleted in Python 4\nunicode_array = array.array(&#039;u&#039;, &#x5B;&#039;\\u0394&#039;, &#039;\\u2167&#039;, &#039;\\u007B&#039;])\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">2. Printing Array and its Type<\/h3>\n\n\n\n<p>If we print the array object, it gives us information about the typecode and its elements. Let&#8217;s print the arrays created above and also print the object type using the type() built-in function.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n# printing array\nprint(int_array)\nprint(float_array)\nprint(unicode_array)\nprint(type(int_array))\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;1, 2, 3, 4])\narray(&#039;f&#039;, &#x5B;1.100000023841858, 2.200000047683716, 3.299999952316284, 4.400000095367432])\narray(&#039;u&#039;, &#039;\u0394\u2167{&#039;)\n&lt;class &#039;array.array&#039;&gt;\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">3. Printing Array Elements<\/h3>\n\n\n\n<p>We can print array elements using the <a rel=\"noreferrer noopener\" aria-label=\"for loop (opens in a new tab)\" href=\"https:\/\/www.askpython.com\/python\/python-for-loop\" target=\"_blank\">for loop<\/a>.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport array\n\nint_array = array.array(&#039;i&#039;, &#x5B;1, 2, 3, 4])\n\nfor a in int_array:\n    print(a)\n<\/pre><\/div>\n\n\n<p>We can also access an element using its index. We can use the indices to print the array elements.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nfor b in range(0, len(int_array)):\n    print(f&#039;int_array&#x5B;{b}] = {int_array&#x5B;b]}&#039;)\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=\"\">\nint_array&#x5B;0] = 1\nint_array&#x5B;1] = 2\nint_array&#x5B;2] = 3\nint_array&#x5B;3] = 4\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">4. Inserting and Appending Elements<\/h3>\n\n\n\n<p>We can use the insert() function to insert an element at the specified index. The elements from the specified index are shifted to right by one position.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nint_array = array.array(&#039;i&#039;, &#x5B;1, 2, 3, 4])\nint_array.insert(0, -1)  # -1,1,2,3,4\nint_array.insert(2, -2)  # -1,1,-2,2,3,4\nprint(int_array)\n<\/pre><\/div>\n\n\n<p><strong>Output<\/strong>: <code>array('i', [-1, 1, -2, 2, 3, 4])<\/code><\/p>\n\n\n\n<p>If you have to add an element at the end of the array, use append() function.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nint_array = array.array(&#039;i&#039;, &#x5B;1, 2, 3, 4])\nint_array.append(-3)\nprint(int_array)  # array(&#039;i&#039;, &#x5B;1, 2, 3, 4, -3])\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">5. Python array supports negative index<\/h3>\n\n\n\n<p>We can access python array elements through negative index too.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"290\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/09\/python-array-index-1024x290.png\" alt=\"Python Array Index\" class=\"wp-image-678\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/09\/python-array-index-1024x290.png 1024w, https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/09\/python-array-index-300x85.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/09\/python-array-index-768x218.png 768w, https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/09\/python-array-index.png 1136w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>Python Array Index<\/figcaption><\/figure><\/div>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nint_array = array.array(&#039;i&#039;, &#x5B;10, 20, 30, 40, 50, 60, 70, 80])\nprint(int_array&#x5B;-2])  # 70\nprint(int_array&#x5B;-5])  # 40\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">6. Removing Array Elements<\/h3>\n\n\n\n<p>We can use remove() method to remove an array element.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nint_array = array.array(&#039;i&#039;, &#x5B;1, 2, 3, 4])\nint_array.remove(2)\nprint(int_array)  # array(&#039;i&#039;, &#x5B;1, 3, 4])\n<\/pre><\/div>\n\n\n<p>If the element is not present in the array, <em>ValueError<\/em> is raised.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nint_array = array.array(&#039;i&#039;, &#x5B;1, 2, 3, 4])\ntry:\n    int_array.remove(20)\nexcept ValueError as ve:\n    print(ve)\n<\/pre><\/div>\n\n\n<p><strong>Output<\/strong>: <code>array.remove(x): x not in array<\/code><\/p>\n\n\n\n<p>We can also use pop() function to remove an element at the given index. This function returns the element being removed from the array. If we don&#8217;t specify the index, the last element is removed and returned.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nint_array = array.array(&#039;i&#039;, &#x5B;1, 2, 3, 4])\nlast_element = int_array.pop()\nprint(last_element)  # 4\nprint(int_array)  # array(&#039;i&#039;, &#x5B;1, 2, 3])\n\nsecond_element = int_array.pop(1)\nprint(second_element)  # 2\nprint(int_array)  # array(&#039;i&#039;, &#x5B;1, 3])\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">7. Slicing an Array<\/h3>\n\n\n\n<p>Python array supports slicing and returns a new array with the sub-elements. The original array remains unchanged. The slicing also supports negative indices.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nint_array = array.array(&#039;i&#039;, &#x5B;0, 1, 2, 3, 4, 5])\nprint(int_array&#x5B;3:])  # array(&#039;i&#039;, &#x5B;3, 4, 5])\nprint(int_array&#x5B;:2])  # array(&#039;i&#039;, &#x5B;0, 1])\nprint(int_array&#x5B;1:3])  # array(&#039;i&#039;, &#x5B;1, 2])\n\n# negative index slicing\nprint(int_array&#x5B;-2:])  # array(&#039;i&#039;, &#x5B;4, 5])\nprint(int_array&#x5B;:-2])  # array(&#039;i&#039;, &#x5B;0, 1, 2, 3])\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">8. Searching an Element in the Array<\/h3>\n\n\n\n<p>We can use the index() function to find the index of first occurrence of an element. If the element is not present in the array, ValueError is raised.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nint_array = array.array(&#039;i&#039;, &#x5B;0, 1, 2, 3, 1, 2])\n\nprint(f&#039;1 is found at index {int_array.index(1)}&#039;)\ntry:\n    print(int_array.index(20))\nexcept ValueError as ve:\n    print(ve)\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=\"\">\n1 is found at index 1\narray.index(x): x not in array\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">9. Updating Value at Specified Index<\/h3>\n\n\n\n<p>We can use the array index with the assignment operator to update the value at an index. If the index is invalid, IndexError is raised.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nint_array = array.array(&#039;i&#039;, &#x5B;0, 1, 2, 3, 1, 2])\n\nint_array&#x5B;0] = -1\nint_array&#x5B;1] = -2\nprint(int_array)\n\ntry:\n    int_array&#x5B;10] = -10\nexcept IndexError as ie:\n    print(ie)\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;-1, -2, 2, 3, 1, 2])\narray assignment index out of range\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">10. Reversing an Array<\/h3>\n\n\n\n<p>We can use the reverse() function to reverse the array elements.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nint_array = array.array(&#039;i&#039;, &#x5B;0, 1, 2, 3])\nint_array.reverse()\nprint(int_array)  # array(&#039;i&#039;, &#x5B;3, 2, 1, 0])\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">11. Count of the Occurrence of an Element<\/h3>\n\n\n\n<p>We can use the count() function to get the number of occurrences of a value in the array.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nint_array = array.array(&#039;i&#039;, &#x5B;0, 1, 1, 0])\nprint(int_array.count(1))  # 2\nprint(int_array.count(10))  # 0\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">12. Extending an Array by Appending an Iterable<\/h3>\n\n\n\n<p>We can use the extend() function to append values from the iterable to the end of the array.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\narray1 = array.array(&#039;i&#039;, &#x5B;0, 1])\narray2 = array.array(&#039;i&#039;, &#x5B;2, 3, 4])\n\narray1.extend(array2)\nprint(array1)  # array(&#039;i&#039;, &#x5B;0, 1, 2, 3, 4])\n\nprint(array2)  # array(&#039;i&#039;, &#x5B;2, 3, 4])\narray2.extend(&#x5B;1, 2])\nprint(array2)  # array(&#039;i&#039;, &#x5B;2, 3, 4, 1, 2])\n\narray1 = array.array(&#039;i&#039;, &#x5B;1])\narray1.extend(set(&#x5B;0,0,0,2]))\nprint(array1)  # array(&#039;i&#039;, &#x5B;1, 0, 2])\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">13. Converting Array to List<\/h3>\n\n\n\n<p>We can use the tolist() function to convert an array to a list.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nint_array = array.array(&#039;i&#039;, &#x5B;0, 1, 2, 3])\nprint(int_array.tolist())  # &#x5B;0, 1, 2, 3]\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>Python array module helps us in creating arrays for integers and float. But, we can perform the same operations with a list. So you should use the array module only when you want the data to be constrained to the given type.<\/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:\/\/docs.python.org\/3.7\/library\/array.html\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\"array module (opens in a new tab)\">array module<\/a><\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Python doesn&#8217;t have explicit array data structure. It&#8217;s because we can do the same things with the List. The list contains a collection of items and it supports add\/update\/delete\/search operations. That&#8217;s why there is not much use of a separate data structure in Python to support arrays. An array contains items of the same type [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[],"class_list":["post-674","post","type-post","status-publish","format-standard","hentry","category-array"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/674","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\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=674"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/674\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=674"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=674"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=674"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}