

{"id":123253,"date":"2023-11-02T17:24:59","date_gmt":"2023-11-02T11:54:59","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=123253"},"modified":"2024-02-28T11:49:45","modified_gmt":"2024-02-28T06:19:45","slug":"python-program-on-array-methods","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/python-program-on-array-methods\/","title":{"rendered":"Python Program on Array Methods"},"content":{"rendered":"<p>In this article, we will explore a Python program that demonstrates various methods of the array module. The array module provides an efficient way to store and manipulate arrays in Python. The program covers essential methods like pop(), tolist(), append(), index(), remove(), count(), reverse(), and insert(). Understanding these methods is crucial for effective array manipulation in Python.<\/p>\n<h2>Prerequisites<\/h2>\n<ul>\n<li>Familiarity with arrays.<\/li>\n<li>Understanding of array methods.<\/li>\n<li>Knowledge of taking user input in Python.<\/li>\n<\/ul>\n<h3>Topic Explanation<\/h3>\n<p>This program commences by initializing an integer array using the array module, setting the stage for exploring its functionalities. It demonstrates various methods within the module to alter and handle the array efficiently. These include pop(), which eliminates the array&#8217;s last element, and tolist(), a method for transforming the entire array into a list format. Other methods like append() are used for incorporating new elements into the array.<\/p>\n<p>Further, the program explores additional capabilities of the array module. It employs the index() function to determine the position of a specific element, and remove() to eliminate an element&#8217;s first occurrence. For tallying how many times an element appears, count() is utilized. Additionally, reverse() facilitates the inversion of the array&#8217;s order, while insert() allows for the addition of elements at designated positions, enhancing the array&#8217;s versatility.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\"># Methods of Array module\r\nimport array as arr\r\n\r\n# Creating an array of integers\r\nmyarr = arr.array('i', [10, 20, 30, 6, 77, 55, 88, 66, 99, 44, 1001])\r\n\r\n# Printing the array\r\nprint(myarr)\r\n\r\n# The below line of code removes the last element from myarr using pop()\r\nmyarr.pop()\r\n\r\n# Converting the array to a list\r\nmylist = myarr.tolist()\r\n\r\n# Appending \"Data Flair\" to the list\r\nmylist.append(\"Data Flair\")\r\n\r\n# Printing the modified list\r\nprint(mylist)\r\n\r\n# Finding the index of the first occurrence of 77 in the array\r\nprint(mylist.index(77))  # Use mylist instead of myarr\r\n\r\n# Displaying the length of the array before removing an element\r\nprint(\"Before remove \", len(mylist))  # Use mylist instead of myarr\r\n\r\n# Removing the first occurrence of 77 from the list\r\nmylist.remove(77)  # Use mylist instead of myarr\r\n\r\n# Displaying the length of the array after removing an element\r\nprint(\"After remove \", len(mylist))  # Use mylist instead of myarr\r\n\r\n# Printing the modified array\r\nprint(mylist)\r\n\r\n# Taking user input for the limit of the array\r\nn = int(input(\"Input size of array\"))\r\n\r\n# Taking user input for array elements\r\nprint(\"Enter elements in the array\")\r\nfor i in range(n):\r\n    x = int(input())\r\n    mylist.append(x)  # Use mylist instead of myarr\r\n\r\n# Taking user input for a number to search in the array\r\ns = int(input(\"Enter a number for search\"))\r\n\r\n# Counting the occurrences of the specified number in the list\r\nn = mylist.count(s)  # Use mylist instead of myarr\r\n\r\n# Checking if the number was found or not\r\nif n &gt; 0:\r\n    print(\"Searching success\")\r\nelse:\r\n    print(\"Searching not success\")\r\n\r\n# Reversing the list\r\nmylist.reverse()  # Use mylist instead of myarr\r\n\r\n# Inserting the number 720 at the beginning of the list\r\nmylist.insert(0, 720)  # Use mylist instead of myarr\r\n\r\n# Counting the occurrences of the number 720 in the list\r\nm = mylist.count(720)  # Use mylist instead of myarr\r\n\r\n# Printing the modified list\r\nprint(mylist)<\/pre>\n<div class=\"df-code-out\">\n<p><strong>Output:<\/strong><\/p>\n<p>array(&#8216;i&#8217;, [10, 20, 30, 6, 77, 55, 88, 66, 99, 44, 1001])<br \/>\n[10, 20, 30, 6, 77, 55, 88, 66, 99, 44, &#8216;Data Flair&#8217;]<br \/>\n4<br \/>\nBefore remove 11<br \/>\nAfter remove 10<br \/>\n[10, 20, 30, 6, 55, 88, 66, 99, 44, &#8216;Data Flair&#8217;]<br \/>\nInput size of array4<br \/>\nEnter elements in the array<br \/>\n2<br \/>\n3<br \/>\n4<br \/>\n5<br \/>\nEnter a number for search5<br \/>\nSearching success<br \/>\n[720, 5, 4, 3, 2, &#8216;Data Flair&#8217;, 44, 99, 66, 88, 55, 6, 30, 20, 10]<\/p>\n<h3>Code Explanation:<\/h3>\n<ul>\n<li>Creates an array of integers called myarr with predefined values<\/li>\n<li>Prints out myarr to show the array contents<\/li>\n<li>Uses myarr.pop() to remove the last element from myarr<\/li>\n<li>Converts myarr to a list called mylist<\/li>\n<li>Appends &#8220;Data Flair&#8221; string to mylist<\/li>\n<li>Prints the modified mylist<\/li>\n<li>Gets index of first occurrence of 77 in mylist<\/li>\n<li>Prints mylist length before and after removing 77<\/li>\n<li>Removes first occurrence of 77 from mylist<\/li>\n<li>Takes user input for array size and elements to append<\/li>\n<li>Searches mylist for user input number<\/li>\n<li>Prints success or not based on search result<\/li>\n<li>Reverses order of mylist<\/li>\n<li>Inserts 720 at beginning of mylist<\/li>\n<li>Counts occurrences of 720 in mylist<\/li>\n<li>Prints final modified mylist<\/li>\n<\/ul>\n<h3>Summary<\/h3>\n<p>In summary, this Python program offers a practical example of using methods from the array module for array manipulation. It demonstrates how these methods can be applied to create, modify, and search arrays efficiently. Understanding the array module and its methods enhances one&#8217;s ability to work with arrays in Python, making data manipulation more convenient and efficient.<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this article, we will explore a Python program that demonstrates various methods of the array module. The array module provides an efficient way to store and manipulate arrays in Python. The program covers&#46;&#46;&#46;<\/p>\n","protected":false},"author":581,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[28680,10333,28678,28626,28679],"class_list":["post-123253","post","type-post","status-publish","format-standard","hentry","category-python","tag-array-methods-in-python","tag-python","tag-python-array-methods","tag-python-practical","tag-python-program-on-array-methods"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Python Program on Array Methods - DataFlair<\/title>\n<meta name=\"description\" content=\"This Python program offers a practical example of using methods from the array module for array manipulation.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/python-program-on-array-methods\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Program on Array Methods - DataFlair\" \/>\n<meta property=\"og:description\" content=\"This Python program offers a practical example of using methods from the array module for array manipulation.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/python-program-on-array-methods\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2023-11-02T11:54:59+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-02-28T06:19:45+00:00\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python Program on Array Methods - DataFlair","description":"This Python program offers a practical example of using methods from the array module for array manipulation.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/python-program-on-array-methods\/","og_locale":"en_US","og_type":"article","og_title":"Python Program on Array Methods - DataFlair","og_description":"This Python program offers a practical example of using methods from the array module for array manipulation.","og_url":"https:\/\/data-flair.training\/blogs\/python-program-on-array-methods\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2023-11-02T11:54:59+00:00","article_modified_time":"2024-02-28T06:19:45+00:00","author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/python-program-on-array-methods\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/python-program-on-array-methods\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/c187795dc82ab948373cca526df7c445"},"headline":"Python Program on Array Methods","datePublished":"2023-11-02T11:54:59+00:00","dateModified":"2024-02-28T06:19:45+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/python-program-on-array-methods\/"},"wordCount":416,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"keywords":["array methods in python","Python","python array methods","python practical","python program on array methods"],"articleSection":["Python Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/python-program-on-array-methods\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/python-program-on-array-methods\/","url":"https:\/\/data-flair.training\/blogs\/python-program-on-array-methods\/","name":"Python Program on Array Methods - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"datePublished":"2023-11-02T11:54:59+00:00","dateModified":"2024-02-28T06:19:45+00:00","description":"This Python program offers a practical example of using methods from the array module for array manipulation.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/python-program-on-array-methods\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/python-program-on-array-methods\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/python-program-on-array-methods\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Python Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/python\/"},{"@type":"ListItem","position":3,"name":"Python Program on Array Methods"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/c187795dc82ab948373cca526df7c445","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/2302ebc438084d2f1f993edc1996a0aae01332e81f3227cba8df0c48ec010ca4?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/2302ebc438084d2f1f993edc1996a0aae01332e81f3227cba8df0c48ec010ca4?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/2302ebc438084d2f1f993edc1996a0aae01332e81f3227cba8df0c48ec010ca4?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"DataFlair Team provides high-impact content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. We make complex concepts easy to grasp, helping learners of all levels succeed in their tech careers.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam6\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/123253","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/581"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=123253"}],"version-history":[{"count":3,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/123253\/revisions"}],"predecessor-version":[{"id":134138,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/123253\/revisions\/134138"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=123253"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=123253"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=123253"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}