{"id":8139,"date":"2024-04-11T12:03:23","date_gmt":"2024-04-11T12:03:23","guid":{"rendered":"https:\/\/www.skillvertex.com\/blog\/?p=8139"},"modified":"2024-04-11T12:03:23","modified_gmt":"2024-04-11T12:03:23","slug":"python-list-methods","status":"publish","type":"post","link":"https:\/\/www.skillvertex.com\/blog\/python-list-methods\/","title":{"rendered":"Python List Methods"},"content":{"rendered":"\n<div class=\"wp-block-rank-math-toc-block\" id=\"rank-math-toc\" id=\"rank-math-toc\"><p>Table of Contents<\/p><nav><div><div ><a href=\"#what-is-python-list-methods\">What is Python List Methods?<\/a><\/div><div ><a href=\"#how-to-add-elements-to-the-list\">How to add Elements to the List?<\/a><\/div><div ><a href=\"#1-python-append-method\">1. Python append() method<\/a><\/div><div ><a href=\"#2-python-insert-method\">2. Python insert() method<\/a><\/div><div ><a href=\"#3-python-extend-method\">3. Python extend() method<\/a><\/div><div ><a href=\"#what-are-the-important-functions-of-the-python-list\">What are the important functions of the Python List?<\/a><div><div ><a href=\"#1-python-sum-method\">1. Python sum() method<\/a><\/div><div ><a href=\"#2-python-count-method\">2. Python count() method<\/a><\/div><div ><a href=\"#3-python-index-method\">3.Python index() method<\/a><\/div><div ><a href=\"#4-python-min-method\">4. Python min() method<\/a><\/div><div ><a href=\"#5-python-max-method\">5. Python max() method<\/a><\/div><div ><a href=\"#6-python-sort-method\">6. Python sort() method <\/a><\/div><div ><a href=\"#7-python-reverse-method\">7. Python reverse() method<\/a><\/div><\/div><\/div><div ><a href=\"#conclusion\">Conclusion<\/a><\/div><div ><a href=\"#python-list-methods-fa-qs\">Python List Methods-FAQs<\/a><\/div><\/div><\/nav><\/div>\n\n\n\n<p>The <a href=\"https:\/\/www.skillvertex.com\/blog\/python-access-list-items\/\" data-type=\"post\" data-id=\"8035\">Python <\/a>List Method will allow you to built-in methods and will do operations on the Python list\/ arrays. Read this article to learn more about Python list Methods.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-python-list-methods\">What is Python List Methods?<\/h2>\n\n\n\n<p><a href=\"https:\/\/www.skillvertex.com\/blog\/python-list-comprehension\/\" data-type=\"post\" data-id=\"8090\">Python <\/a>consists of a set of <a href=\"https:\/\/www.skillvertex.com\/blog\/python-built-in-functions\/\" data-type=\"post\" data-id=\"7464\">built-in method<\/a>s that will be used on lists.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>S.no<\/th><th>Method<\/th><th>Description<\/th><\/tr><\/thead><tbody><tr><td>1<\/td><td><em>append<\/em>()<\/td><td>append() will work to add elements to the end of the List.&nbsp;<\/td><\/tr><tr><td>2<\/td><td><em><a href=\"https:\/\/www.skillvertex.com\/blog\/python-copy-list\/\" data-type=\"post\" data-id=\"8109\">copy<\/a><\/em>()<\/td><td>Copy() will return a shallow copy of a list<\/td><\/tr><tr><td>3<\/td><td><em>clear<\/em>()<\/td><td>clear() method is used for removing all items from the list.&nbsp;<\/td><\/tr><tr><td>4<\/td><td><em>count<\/em>()<\/td><td>count() method will count the elements.<\/td><\/tr><tr><td>5<\/td><td><em>extend<\/em>()<\/td><td>extend() is used to add each element of an iterable to the end of the List.<\/td><\/tr><tr><td>6<\/td><td><em>index<\/em>()<\/td><td>index() will return the lowest index where the element appears.&nbsp;<\/td><\/tr><tr><td>7<\/td><td><em><a href=\"https:\/\/www.skillvertex.com\/blog\/python-add-list-items\/\" data-type=\"post\" data-id=\"8058\">insert<\/a><\/em>()<\/td><td>This method will insert a given element at a given index in a list.&nbsp;<\/td><\/tr><tr><td>8<\/td><td><em>pop<\/em>()<\/td><td>This will remove and return the last value from the List or the given index value.<\/td><\/tr><tr><td>9<\/td><td><em>remove<\/em>()<\/td><td>It will remove the given object from the List.&nbsp;<\/td><\/tr><tr><td>10<\/td><td><em>reverse<\/em>()<\/td><td>It will reverse objects of the List in place.<\/td><\/tr><tr><td>11<\/td><td><em>sort<\/em>()<\/td><td>Sort() will sort the  List in ascending, descending, or user-defined order.<\/td><\/tr><tr><td>12<\/td><td><em>min<\/em>()<\/td><td>min() will calculate the minimum of all the elements of the List<\/td><\/tr><tr><td>13<\/td><td><em>max<\/em>()<\/td><td>max will calculates the maximum of all the elements of the List<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"how-to-add-elements-to-the-list\">How to add Elements to the List?<\/h2>\n\n\n\n<p> The built-in  Python methods are used to add the <a href=\"https:\/\/www.skillvertex.com\/blog\/which-one-is-not-an-element-of-iot\/\" data-type=\"post\" data-id=\"3089\">element <\/a>to the list.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"1-python-append-method\">1. Python append() method<\/h2>\n\n\n\n<p>This method is used to add the element to the end of the <a href=\"https:\/\/www.skillvertex.com\/blog\/python-list-sort-method\/\" data-type=\"post\" data-id=\"8100\">list<\/a>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Syntax: list.append (element)<\/code><\/pre>\n\n\n\n<p>Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class CustomList:\n    def __init__(self):\n        self.data = &#91;]\n\n    def append(self, value):\n        self.data += &#91;value]\n\n    def __str__(self):\n        return str(self.data)\n\n\n# Example usage\ncustom_list = CustomList()\ncustom_list.append(1)\ncustom_list.append(2)\ncustom_list.append(3)\n\nprint(\"Custom list after appending elements:\", custom_list)\n<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Custom list after appending elements: &#91;1, 2, 3]\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"2-python-insert-method\">2. Python insert() method<\/h2>\n\n\n\n<p>The Python insert() method will allow you to insert the element at the specified position.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Syntax:\n\nlist.insert(&lt;position, element)<\/code><\/pre>\n\n\n\n<p>Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class CustomList:\n    def __init__(self):\n        self.data = &#91;]\n\n    def insert(self, index, value):\n        self.data = self.data&#91;:index] + &#91;value] + self.data&#91;index:]\n\n    def __str__(self):\n        return str(self.data)\n\n\n# Example usage\ncustom_list = CustomList()\ncustom_list.insert(0, 1)\ncustom_list.insert(1, 2)\ncustom_list.insert(1, 3)\n\nprint(\"Custom list after inserting elements:\", custom_list)\n<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Custom list after inserting elements: &#91;1, 3, 2]\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"3-python-extend-method\">3. Python extend() method<\/h2>\n\n\n\n<p>The Python extend method will add the items of the iterable to the end of the list.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Syntax: List1.extend(List2)<\/code><\/pre>\n\n\n\n<p>Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class CustomList:\n    def __init__(self):\n        self.data = &#91;]\n\n    def extend(self, iterable):\n        self.data += iterable\n\n    def __str__(self):\n        return str(self.data)\n\n\n# Example usage\ncustom_list = CustomList()\ncustom_list.extend(&#91;1, 2, 3])\ncustom_list.extend(&#91;4, 5, 6])\n\nprint(\"Custom list after extending elements:\", custom_list)\n<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Custom list after extending elements: &#91;1, 2, 3, 4, 5, 6]\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-are-the-important-functions-of-the-python-list\">What are the important functions of the Python List?<\/h2>\n\n\n\n<p>Some of the important functions of the Python list are given below:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"1-python-sum-method\">1. Python sum() method<\/h3>\n\n\n\n<p>The Python sum method will allow you to calculate the sum of the elements of the list.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Syntax: sum(List)\n\n<\/code><\/pre>\n\n\n\n<p>Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class CustomList:\n    def __init__(self):\n        self.data = &#91;]\n\n    def append(self, value):\n        self.data += &#91;value]\n\n    def sum(self):\n        return sum(self.data)\n\n    def __str__(self):\n        return str(self.data)\n\n\n# Example usage\ncustom_list = CustomList()\ncustom_list.append(1)\ncustom_list.append(2)\ncustom_list.append(3)\n\nprint(\"Custom list:\", custom_list)\nprint(\"Sum of elements in custom list:\", custom_list.sum())\n<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Custom list: &#91;1, 2, 3]\nSum of elements in custom list: 6\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"2-python-count-method\">2. Python count() method<\/h3>\n\n\n\n<p>This method will sum the total occurrence of the given element of the <a href=\"https:\/\/www.skillvertex.com\/blog\/python-list-sort-method\/\" data-type=\"post\" data-id=\"8100\">list<\/a>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Syntax: List.count(element)<\/code><\/pre>\n\n\n\n<p>Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class CustomList:\n    def __init__(self):\n        self.data = &#91;]\n\n    def append(self, value):\n        self.data.append(value)\n\n    def count(self, value):\n        return self.data.count(value)\n\n    def __str__(self):\n        return str(self.data)\n\n\n# Example usage\ncustom_list = CustomList()\ncustom_list.append(1)\ncustom_list.append(2)\ncustom_list.append(3)\ncustom_list.append(1)  # Adding one more 1\n\nprint(\"Custom list:\", custom_list)\nprint(\"Number of occurrences of '1' in custom list:\", custom_list.count(1))\n<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Custom list: &#91;1, 2, 3, 1]\nNumber of occurrences of '1' in custom list: 2\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"3-python-index-method\">3.Python index() method<\/h3>\n\n\n\n<p>This method will return the index of the first occurrence. So, the start and end indexes are not the required parameters.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Syntax: List.index(element&#91;,start&#91;,end]])<\/code><\/pre>\n\n\n\n<p><strong>Example<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class CustomList:\n    def __init__(self):\n        self.data = &#91;]\n\n    def append(self, value):\n        self.data.append(value)\n\n    def index(self, value):\n        return self.data.index(value)\n\n    def __str__(self):\n        return str(self.data)\n\n\n# Example usage\ncustom_list = CustomList()\ncustom_list.append(1)\ncustom_list.append(2)\ncustom_list.append(3)\n\nprint(\"Custom list:\", custom_list)\nprint(\"Index of '2' in custom list:\", custom_list.index(2))\n<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Custom list: &#91;1, 2, 3]\nIndex of '2' in custom list: 1\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"4-python-min-method\">4. Python min() method<\/h3>\n\n\n\n<p>This method will find the minimum of all the elements of the list.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Syntax: min(iterable, *iterables&#91;, key])<\/code><\/pre>\n\n\n\n<p>Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class CustomList:\n    def __init__(self):\n        self.data = &#91;]\n\n    def append(self, value):\n        self.data.append(value)\n\n    def min(self):\n        return min(self.data)\n\n    def __str__(self):\n        return str(self.data)\n\n\n# Example usage\ncustom_list = CustomList()\ncustom_list.append(3)\ncustom_list.append(1)\ncustom_list.append(5)\ncustom_list.append(2)\n\nprint(\"Custom list:\", custom_list)\nprint(\"Minimum value in custom list:\", custom_list.min())\n<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Custom list: &#91;3, 1, 5, 2]\nMinimum value in custom list: 1\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"5-python-max-method\">5. Python max() method<\/h3>\n\n\n\n<p>The Python will calculate the maximum of all the elements of the list.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Syntax: max(iterable, *iterables&#91;, key])\n\n<\/code><\/pre>\n\n\n\n<p>Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class CustomList:\n    def __init__(self):\n        self.data = &#91;]\n\n    def append(self, value):\n        self.data.append(value)\n\n    def max(self):\n        return max(self.data)\n\n    def __str__(self):\n        return str(self.data)\n\n\n# Example usage\ncustom_list = CustomList()\ncustom_list.append(3)\ncustom_list.append(1)\ncustom_list.append(5)\ncustom_list.append(2)\n\nprint(\"Custom list:\", custom_list)\nprint(\"Maximum value in custom list:\", custom_list.max())\n<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Custom list: &#91;3, 1, 5, 2]\nMaximum value in custom list: 5\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"6-python-sort-method\">6. Python sort() method <\/h3>\n\n\n\n<p>This <a href=\"https:\/\/www.skillvertex.com\/blog\/which-of-the-following-are-methods-traffic-manager-uses-to-pick-endpoints-in-azure\/\" data-type=\"post\" data-id=\"3218\">method <\/a>will allow us to sort the given <a href=\"https:\/\/www.skillvertex.com\/blog\/mern-stack-vs-data-science\/\" data-type=\"post\" data-id=\"4251\">data <\/a>structure in ascending order. Whereas, the key and reverse flag won&#8217;t be considered as the necessary <a href=\"https:\/\/www.skillvertex.com\/blog\/what-are-the-limitations-of-parameters-in-tableau\/\" data-type=\"post\" data-id=\"3022\">parameter <\/a>and the reverse_ flag will be set to false when nothing is passed through the sorted().<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Syntax: list.sort(&#91;key,&#91;Reverse_flag]])<\/code><\/pre>\n\n\n\n<p>Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class CustomList:\n    def __init__(self):\n        self.data = &#91;]\n\n    def append(self, value):\n        self.data.append(value)\n\n    def sort(self):\n        self.data.sort()\n\n    def __str__(self):\n        return str(self.data)\n\n\n# Example usage\ncustom_list = CustomList()\ncustom_list.append(3)\ncustom_list.append(1)\ncustom_list.append(5)\ncustom_list.append(2)\n\nprint(\"Custom list before sorting:\", custom_list)\ncustom_list.sort()\nprint(\"Custom list after sorting:\", custom_list)\n<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Custom list before sorting: &#91;3, 1, 5, 2]\nCustom list after sorting: &#91;1, 2, 3, 5]\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"7-python-reverse-method\">7. Python reverse() method<\/h3>\n\n\n\n<p>The <a href=\"https:\/\/www.skillvertex.com\/blog\/how-to-reverse-a-string-in-java\/\" data-type=\"post\" data-id=\"728\">reverse <\/a>method will reverse the order of the list.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Syntax: list. reverse()<\/code><\/pre>\n\n\n\n<p>Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class CustomList:\n    def __init__(self):\n        self.data = &#91;]\n\n    def append(self, value):\n        self.data.append(value)\n\n    def reverse(self):\n        self.data = self.data&#91;::-1]\n\n    def __str__(self):\n        return str(self.data)\n\n\n# Example usage\ncustom_list = CustomList()\ncustom_list.append(1)\ncustom_list.append(2)\ncustom_list.append(3)\n\nprint(\"Custom list before reversing:\", custom_list)\ncustom_list.reverse()\nprint(\"Custom list after reversing:\", custom_list)\n<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Custom list before reversing: &#91;1, 2, 3]\nCustom list after reversing: &#91;3, 2, 1]\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"conclusion\">Conclusion<\/h2>\n\n\n\n<p>Python lists are fundamental data structures that enable the storage and manipulation of collections of elements. For beginners learning Python, grasping basic list methods is crucial. These methods offer essential functionalities for managing lists effectively. <\/p>\n\n\n\n<p>However, the append() method will allow adding elements to the end of a list, while extend() allows adding multiple elements from another iterable. insert() permits inserting elements at specific positions. For removing elements, students can utilize remove() to delete the first occurrence of a value or pop() to remove and return an element at a given index. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"python-list-methods-fa-qs\">Python List Methods-FAQs<\/h2>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1710419388858\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q1.How do I list available methods in Python?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. using the dir() in Python is used to list all the methods of the class.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1710419396152\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q2.What are the Python methods?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. The three methods of Python include the Instance Method, Class Method, and Static Method.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1710419406397\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q3.What is the use of list () method?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans.The list() function will convert the iterable such as a string, tuple, or dictionary into the list.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>The Python List Method will allow you to built-in methods and will do operations on the Python list\/ arrays. Read this article to learn more about Python list Methods. What is Python List Methods? Python consists of a set of built-in methods that will be used on lists. S.no Method Description 1 append() append() will &#8230; <a title=\"Python List Methods\" class=\"read-more\" href=\"https:\/\/www.skillvertex.com\/blog\/python-list-methods\/\" aria-label=\"More on Python List Methods\">Read more<\/a><\/p>\n","protected":false},"author":4,"featured_media":8144,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[864],"tags":[57,945,976,866],"class_list":["post-8139","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-tutorial","tag-python","tag-python-function","tag-python-list-methods","tag-python-tutorial","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-33"],"_links":{"self":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/8139","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/comments?post=8139"}],"version-history":[{"count":7,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/8139\/revisions"}],"predecessor-version":[{"id":8914,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/8139\/revisions\/8914"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media\/8144"}],"wp:attachment":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media?parent=8139"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/categories?post=8139"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/tags?post=8139"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}