{"id":8773,"date":"2021-03-08T18:04:22","date_gmt":"2021-03-08T12:34:22","guid":{"rendered":"https:\/\/www.h2kinfosys.com\/blog\/?p=8773"},"modified":"2021-03-08T18:06:06","modified_gmt":"2021-03-08T12:36:06","slug":"python-arrays-create-reverse-remove-element","status":"publish","type":"post","link":"https:\/\/www.h2kinfosys.com\/blog\/python-arrays-create-reverse-remove-element\/","title":{"rendered":"Python Arrays"},"content":{"rendered":"\n<p>An array in Python is simply a collection of data in a particular data type. Lists in some sense can be seen as arrays, only that the elements of a list can contain more than one data type. Arrays do not support multiple data types. In Python, arrays are created using the array module which must be specifically imported.&nbsp;<\/p>\n\n\n\n<p>Arrays are used for mathematical computations so their elements must be a numerical data type. In this tutorial, we will discuss how to create an <a href=\"https:\/\/www.h2kinfosys.com\/blog\/python-arrays-create-reverse-remove-element\/\" class=\"rank-math-link\">array in Python<\/a> and the various operations that can be done on an array. In specific terms, here\u2019s what you will learn by the end of this tutorial.\u00a0<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Are Arrays Better than List?<\/li><li>Defining an Array in Python<\/li><li>Accessing the Elements of an Array.&nbsp;<\/li><li>Inserting an Element in an Array<\/li><li>How to Change the Element of an Array.&nbsp;<\/li><li>How to Join Arrays Together<\/li><li>How to remove an element from an Array with the pop() method.&nbsp;<\/li><li>Using the remove elements of an Array with the remove() method in Python<\/li><li>How to Reverse the elements of an array in Python<\/li><li>How to Reverse the elements of an array in Python<\/li><\/ul>\n\n\n\n<p>Let\u2019s blast off.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Are Arrays Better than List?<\/h2>\n\n\n\n<p>We have mentioned that a list can be seen as an array only that the elements of a list are not constrained to a specific numeric datatype. In simple terms, arrays are much better than lists for numerical computations. They can do more <a href=\"https:\/\/en.wikipedia.org\/wiki\/Computational_complexity_of_mathematical_operations\" class=\"rank-math-link\" rel=\"nofollow noopener\" target=\"_blank\">complex mathematical operations<\/a> in a faster time. Furthermore, arrays typically consume less system memory than lists.\u00a0<\/p>\n\n\n\n<p>Syntax<\/p>\n\n\n\n<p>According to its official documentation. Here is the Docstring.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Docstring:\u00a0\u00a0\u00a0\u00a0\u00a0\narray(typecode [, initializer]) -> array\n\u00a0\nReturn a new array whose items are restricted by type code, and\ninitialized from the optional initializer value, which must be a list,\nstring or iterable over elements of the appropriate type.\n<\/pre>\n\n\n\n<p>Parameters:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Type code: The type code is the datatype of the arrays that would be defined.&nbsp;<\/li><li>Array: This receives the list of items you wish to store as an array, following the type code you defined.&nbsp;<\/li><\/ul>\n\n\n\n<p>There are various single character representations for different type codes. These type codes will be shown below.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>&nbsp; Type code&nbsp;&nbsp;<\/strong><\/td><td><strong>Minimum size (in bytes)<\/strong><\/td><td><strong>C Type<\/strong><\/td><\/tr><tr><td>&nbsp; &nbsp; &#8216;b&#8217;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/td><td>1<\/td><td>Signed integer<\/td><\/tr><tr><td>&nbsp; &nbsp; &#8216;B&#8217;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/td><td>1<\/td><td>Unsigned integer<\/td><\/tr><tr><td>&nbsp; &nbsp; &#8216;u&#8217;&nbsp;&nbsp;<\/td><td>&nbsp;<\/td><td>Unicode character (Refer to the note)<\/td><\/tr><tr><td>&nbsp; &nbsp; &#8216;h&#8217;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/td><td>2<\/td><td>Signed integer<\/td><\/tr><tr><td>&nbsp; &nbsp; &#8216;H&#8217;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/td><td>2<\/td><td>Unsigned integer<\/td><\/tr><tr><td>&nbsp; &nbsp; &#8216;i&#8217;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/td><td>2<\/td><td>Signed integer<\/td><\/tr><tr><td>&nbsp; &nbsp; &#8216;I&#8217;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/td><td>2<\/td><td>Unsigned integer<\/td><\/tr><tr><td>&nbsp; &nbsp; &#8216;l&#8217;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/td><td>4<\/td><td>Signed integer<\/td><\/tr><tr><td>&nbsp; &nbsp; &#8216;L&#8217;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/td><td>4<\/td><td>Unsigned integer<\/td><\/tr><tr><td>&nbsp; &nbsp; &#8216;q&#8217;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/td><td>8<\/td><td>Signed integer (Refer to note)<\/td><\/tr><tr><td>&nbsp; &nbsp; &#8216;Q&#8217;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/td><td>8<\/td><td>Unsigned integer (Refer to note)<\/td><\/tr><tr><td>&nbsp; &nbsp; &#8216;f&#8217;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/td><td>4<\/td><td>Floating point<\/td><\/tr><tr><td>&nbsp; &nbsp; &#8216;d&#8217;&nbsp;&nbsp;&nbsp;&nbsp;<\/td><td>8<\/td><td>Floating point<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Note:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>The \u2018u\u2019 type code means the Unicode character in Python. It is 2 bytes on the narrow build and 4 bytes on wide builds<\/li><li>Both the \u2018q\u2019 and \u2018Q\u2019 type codes are available if the C compiler upon which the Python code was written supports __int64 on Windows or long long.<\/li><\/ul>\n\n\n\n<p>If you don\u2019t understand what a signed and unsigned data type is, an unsigned data type does not accept negative numbers. In other words, only numbers greater than 0 are permitted. A signed data type in contrast accepts negative numbers. However, the range of positive numbers it can take is reduced to accomodate the negative numbers.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Defining an Array in Python<\/h2>\n\n\n\n<p>An example of an array is shown below.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">#import the necessary libraries\nimport array\n\u00a0\n#define an array\narray1 = array.array('i', [2, 3, 54, 4, 6, 12, -2])\n\u00a0\nprint(array1)<\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Output:\narray('i', &#91;2, 3, 54, 4, 6, 12, -2])\n<\/code><\/pre>\n\n\n\n<p>Note that if the type code was changed to \u2018I\u2019, which is an unsigned data type, the array would not be successfully created because of the -2 item. See the code below.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">#import the necessary libraries\nimport array\n\u00a0\n#define an array\narray1 = array.array('I', [2, 3, 54, 4, 6, 12, -2])\n\u00a0\nprint(array1)\n<\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Output:\nTraceback (most recent call last):\n  File \"c:\/Users\/DELL\/Desktop\/pycodes\/__pycache__\/strings.py\", line 5, in &lt;module>\n    array1 = array.array('I', &#91;2, 3, 54, 4, 6, 12, -2])\nOverflowError: can't convert negative value to unsigned int\n\nAs seen, it throws an error. \n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Accessing the Elements of an Array through Slicing and Indexing.&nbsp;<\/h2>\n\n\n\n<p>Just like in lists, the element of an array can be accessed through indexing. Its syntax is as follows, array_variable[index].&nbsp;<\/p>\n\n\n\n<p>Remember that the indexing in Python begins from 0. If for example, we wish to access the first element of an array, we pass index 0.\u00a0<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">#import the necessary libraries\nimport array\n\u00a0\n#define an array\narray1 = array.array('i', [2, 3, 54, 4, 6, 12, -2])\n\u00a0\nprint(array1[0])\n<\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Output:\n2\n<\/code><\/pre>\n\n\n\n<p>To access a list of elements, the slicing operation is done. Again, just like in list slicing, slicing an array follows the syntax array_name[start_index:end_index]. If we wish to access the second to 6th elements in the array, we can write:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">print(array1[1:5])\n<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">Output:\narray('i', [3, 54, 4, 6])\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Inserting an Element in an Array<\/h2>\n\n\n\n<p>\u00a0You can add one or more elements to an array using the insert() method. The insertion can be done at any point in the array, whether the beginning, middle, or end. The insert method follows the syntax below.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">arrayName.insert(index, value)\u00a0\n<\/pre>\n\n\n\n<p>Parameters:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Index: This is the position where the new element will be added<\/li><li>Value: The is the value of the elements added<\/li><\/ul>\n\n\n\n<p>Example:<\/p>\n\n\n\n<p>Let\u2019s say we wish to add a number to the beginning of an array. The index parameter would be defined as 0. The code below shows how to add 1000 to the beginning of an already defined array.\u00a0<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">#import the necessary libraries\nimport array\n\u00a0\n#define an array\narray1 = array.array('i', [2, 3, 54, 4, 6, 12, -2])\n\u00a0\n#add an element to the array\narray1.insert(0, 1000)\nprint(array1)\n<\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Output:\narray('i', &#91;1000, 2, 3, 54, 4, 6, 12, -2])\n\nAs seen, 1000 is now the first element in the array. \n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">How to Change the Element of an Array.&nbsp;<\/h2>\n\n\n\n<p>Arrays are mutable which means you can change the elements in the array dynamically. If you wish to change the elements of an array, you simply access the element through indexing and reassign the value to a different number. See the example below.\u00a0<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">#import the necessary libraries\nimport array\n\u00a0\n#define an array\narray1 = array.array('i', [2, 3, 54, 4, 6, 12, -2])\n\u00a0\n#change the first element from 2 to -10\narray1[0] = -10\nprint(array1)<\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Output:\narray('i', &#91;-10, 3, 54, 4, 6, 12, -2])\n\nAs seen, the first element has been changed from 2 to -10\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">How to Join Arrays Together<\/h2>\n\n\n\n<p>Arrays can be concatenated using the \u2018+\u2019 sign. This operation adds the beginning of the first array to the end of the first array to return one longer array. See the example below.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">#import the necessary libraries\nimport array\n\u00a0\n#define an array\narray1 = array.array('i', [1, 2, 3])\narray2 = array.array('i', [4, 5, 6])\n\u00a0\n#join both arrays\nprint(array1 + array2)<\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Output:\narray('i', &#91;1, 2, 3, 4, 5, 6])\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">How to remove an element from an Array with the pop() method.&nbsp;<\/h2>\n\n\n\n<p>The pop method removes an element from an array. The pop method receives one argument, the index of the element to be removed. By default, the index is set to -1 which means that running the pop method without defining an argument removes the last element from the array.&nbsp;<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">#import the necessary libraries\nimport array\n\u00a0\n#define an array\narray1 = array.array('i', [1, 2, 3])\n\u00a0\n#remove the last element\narray1.pop()\n\u00a0\nprint(array1)\n<\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Output:\narray('i', &#91;1, 2])\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Using the remove elements of an Array with the remove() method in Python<\/h2>\n\n\n\n<p>While the pop method removes elements based on its index, the remove method removes elements based on their value. The remove method expects one argument, the value you wish to remove. It then removes the first occurrence of the value and rearranges the array to replace its index.&nbsp;<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n\n<p>If we wish to remove the first number \u201811\u2019 from an array defined. We can write the shown as shown below.\u00a0<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">#import the necessary libraries\nimport array\n\u00a0\n#define an array\narray1 = array.array('i', [11, 20, 9, 11])\n\u00a0\n#remove the first number 11\narray1.remove(11)\n\u00a0\nprint(array1)\n<\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Output:\narray('i', &#91;20, 9, 11])\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">How to Reverse the elements of an array in Python<\/h2>\n\n\n\n<p>The reverse method is used to reverse the arrangements of elements of an array from back to front. See an example below.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">#import the necessary libraries\nimport array\n\u00a0\n#define an array\narray1 = array.array('i', [11, 20, 9, 81])\n\u00a0\n#remove the last element\narray1.reverse()\n\u00a0\nprint(array1)<\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Output:\narray('i', &#91;81, 9, 20, 11])\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">How to count the number of Occurrences of an Element&nbsp;<\/h2>\n\n\n\n<p>The count method is used to count the number of times an element appears in an array. It receives one argument; the element that needs to be counted.&nbsp;<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">#import the necessary libraries\nimport array\n\u00a0\n#define an array\narray1 = array.array('i', [11, 20, 11, 9, 81, 11])\n\u00a0\n#count the number of times 11 appears\nprint(array1.count(11))<\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Output:\n3\nOf course, 11 appears 3 times in the array. <\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Wrapping up,&nbsp;<\/h2>\n\n\n\n<p>In this tutorial, you have learned how to create arrays in Python from the array module. You also discovered how to perform operations on an array. Operations such as accessing element(s) in an array, adding elements to an array, removing elements from an array, counting the number of occurrences of elements, etc. If you have any questions whatsoever, feel free to leave them in the comment section and I\u2019d do my best to answer them.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>An array in Python is simply a collection of data in a particular data type. Lists in some sense can be seen as arrays, only that the elements of a list can contain more than one data type. Arrays do not support multiple data types. In Python, arrays are created using the array module which [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":8776,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[342],"tags":[],"class_list":["post-8773","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-tutorials"],"_links":{"self":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/8773","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/comments?post=8773"}],"version-history":[{"count":0,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/8773\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media\/8776"}],"wp:attachment":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media?parent=8773"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/categories?post=8773"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/tags?post=8773"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}