{"id":511,"date":"2019-07-23T10:50:55","date_gmt":"2019-07-23T10:50:55","guid":{"rendered":"http:\/\/askpython.com\/?p=511"},"modified":"2023-01-13T07:05:21","modified_gmt":"2023-01-13T07:05:21","slug":"python-list","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/list\/python-list","title":{"rendered":"Python List &#8211; 15 Things You MUST Know"},"content":{"rendered":"\n<p>Python has four built-in data types data, set, tuple, dictionary, and list. These data types can store multiple collections of data.<\/p>\n\n\n\n<p>A set is an unordered collection of data, it is iterable, mutable, and has no duplicate elements. A tuple is an ordered collection of data but unlike a set, it is not mutable. A dictionary is a collection that contains data in key-value pairs. It is also ordered but unlike a tuple, it is mutable and does not allow duplicates.<\/p>\n\n\n\n<p>On the other hand, a list is a typed array that can store duplicated elements, it is an ordered collection of data, it is dynamic in size and mutable, and its elements can be accessed with their index.\u00a0<\/p>\n\n\n\n<p>This tutorial will cover all the fundamental things you must know about the list in Python.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Python List<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The list is a mutable sequence.<\/li>\n\n\n\n<li>We can create it by placing elements inside a square bracket.<\/li>\n\n\n\n<li>The list elements are separated using a comma.<\/li>\n\n\n\n<li>We can also create nested lists.<\/li>\n\n\n\n<li>The list is an ordered collection. So it maintains the order in which elements are added.<\/li>\n\n\n\n<li>We can access its elements using their index value. It also supports the negative index to refer to the elements from end to start.<\/li>\n\n\n\n<li>We can unpack its elements to comma-separated variables.<\/li>\n\n\n\n<li>A List can have duplicate elements. They also allow <strong>None<\/strong>.<\/li>\n\n\n\n<li>It supports two&nbsp;<a href=\"https:\/\/www.askpython.com\/python\/python-operators\" target=\"_blank\" rel=\"noreferrer noopener\">operators<\/a>: + for concatenation and * for repeating the elements.<\/li>\n\n\n\n<li>We can slice a list to create another list from its elements.<\/li>\n\n\n\n<li>We can iterate over list elements using the&nbsp;<a rel=\"noreferrer noopener\" href=\"https:\/\/www.askpython.com\/python\/python-for-loop\" target=\"_blank\">for loop<\/a>.<\/li>\n\n\n\n<li>We can use the &#8220;in&#8221; operator to check whether an item is present in the list. We can also use the &#8220;not in&#8221; operator with a list.<\/li>\n\n\n\n<li>A list is used to store homogeneous elements where we want to add\/update\/delete elements.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Creating a Python List<\/h2>\n\n\n\n<p>A list is created by placing elements inside square brackets, separated by a comma.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nfruits_list = &#x5B;&quot;Apple&quot;, &quot;Banana&quot;, &quot;Orange&quot;]\n<\/pre><\/div>\n\n\n<p>We can keep different types of elements in it.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nrandom_list = &#x5B;1, &quot;A&quot;, object(), 10.55, True, (1, 2)]\n<\/pre><\/div>\n\n\n<p>We can also have nested lists.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nnested_list = &#x5B;1, &#x5B;2, 3], &#x5B;4, 5, 6], 7]\n<\/pre><\/div>\n\n\n<p>We can create an empty list by having no elements inside the square brackets.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nempty_list = &#x5B;]\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"526\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/07\/creating-a-list-in-python-1024x526.png\" alt=\"Creating A List In Python\" class=\"wp-image-513\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/07\/creating-a-list-in-python-1024x526.png 1024w, https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/07\/creating-a-list-in-python-300x154.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/07\/creating-a-list-in-python-768x395.png 768w, https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/07\/creating-a-list-in-python.png 1148w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption class=\"wp-element-caption\">Creating a List in Python<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Accessing List Elements<\/h2>\n\n\n\n<p>We can access list elements using their index value. The index value starts from 0, i.e, the first element has the index number \u20180\u2019, since it starts counting the index from \u20180\u2019 not \u20181\u2019 so you have to subtract 1 to get the exact element, for example, if you want to access the third element of a list you have to use the index \u20182\u2019.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n&gt;&gt;&gt; vowels_list = &#x5B;&quot;a&quot;, &quot;e&quot;, &quot;i&quot;, &quot;o&quot;, &quot;u&quot;]\n&gt;&gt;&gt; vowels_list&#x5B;0]\n&#039;a&#039;\n&gt;&gt;&gt; vowels_list&#x5B;4]\n&#039;u&#039;\n<\/pre><\/div>\n\n\n<p>If the index is not in the range, IndexError is raised.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n&gt;&gt;&gt; vowels_list&#x5B;40]\nTraceback (most recent call last):\n  File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;\nIndexError: list index out of range\n&gt;&gt;&gt; \n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"788\" height=\"372\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/07\/accessing-python-list-elements.png\" alt=\"Accessing Python List Elements\" class=\"wp-image-515\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/07\/accessing-python-list-elements.png 788w, https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/07\/accessing-python-list-elements-300x142.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/07\/accessing-python-list-elements-768x363.png 768w\" sizes=\"auto, (max-width: 788px) 100vw, 788px\" \/><figcaption class=\"wp-element-caption\">Accessing Elements<\/figcaption><\/figure>\n\n\n\n<p>We can also pass a negative index value. In this case, the element is returned from the end to the start. The valid index value range is from -1 to -(list length). <\/p>\n\n\n\n<p>This is useful when we want a specific element quickly, such as the last element, second last element, etc.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n&gt;&gt;&gt; vowels_list = &#x5B;&quot;a&quot;, &quot;e&quot;, &quot;i&quot;, &quot;o&quot;, &quot;u&quot;]\n&gt;&gt;&gt; vowels_list&#x5B;-1]  # last element\n&#039;u&#039;\n&gt;&gt;&gt; vowels_list&#x5B;-2]  # second last element\n&#039;e&#039;\n&gt;&gt;&gt; vowels_list&#x5B;-5]\n&#039;a&#039;\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Accessing Nested List Elements<\/h2>\n\n\n\n<p>We can access nested list elements using nested indexes. Let&#8217;s understand this with some simple examples.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nnested_list = &#x5B;1, &#x5B;2, 3], &#x5B;4, 5, 6], 7]\n\n# first element in the nested sequence at index 1\nprint(nested_list&#x5B;1]&#x5B;0])\n\n# second element in the nested sequence at index 1\nprint(nested_list&#x5B;1]&#x5B;1])\n\n# third element in the nested sequence at index 2\nprint(nested_list&#x5B;2]&#x5B;2])\n<\/pre><\/div>\n\n\n<p>The nested element can be any other sequence also that supports index-based access. For example, the result will be the same for the nested list [1, (2, 3), (4, 5, 6), 7].<\/p>\n\n\n\n<p>We can use negative indexes with nested lists too. The above code snippet can be rewritten as follows.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nnested_list = &#x5B;1, (2, 3), (4, 5, 6), 7]\n\n# first element in the nested sequence at third last index\nprint(nested_list&#x5B;-3]&#x5B;0])\n\n# last element in the nested sequence at third last index\nprint(nested_list&#x5B;-3]&#x5B;-1])\n\n# last element in the nested sequence at second last index\nprint(nested_list&#x5B;-2]&#x5B;-1])\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"288\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/07\/python-list-negative-index-1024x288.png\" alt=\"Python List Negative Index\" class=\"wp-image-518\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/07\/python-list-negative-index-1024x288.png 1024w, https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/07\/python-list-negative-index-300x84.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/07\/python-list-negative-index-768x216.png 768w, https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/07\/python-list-negative-index.png 1132w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption class=\"wp-element-caption\">List Index Values<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Updating a List<\/h2>\n\n\n\n<p>We can use the assignment operator to change the list value at the specified index.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n&gt;&gt;&gt; my_list = &#x5B;1, 2, 3]\n&gt;&gt;&gt; my_list&#x5B;1] = 10\n&gt;&gt;&gt; my_list\n&#x5B;1, 10, 3]\n&gt;&gt;&gt; \n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Iterating through a List<\/h2>\n\n\n\n<p>We can use the for loop to iterate over the elements of a list.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n&gt;&gt;&gt; my_list = &#x5B;1, 2, 3]\n&gt;&gt;&gt; for x in my_list:\n...     print(x)\n... \n1\n2\n3\n&gt;&gt;&gt; \n<\/pre><\/div>\n\n\n<p>If you want to iterate over the list elements in the reverse order, you can use reversed() built-in function.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&gt;&gt;&gt; my_list = &#x5B;1, 2, 3]\n&gt;&gt;&gt; for x in reversed(my_list):\n...     print(x)\n... \n3\n2\n1\n&gt;&gt;&gt; \n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Check if an item exists in the list<\/h2>\n\n\n\n<p>We can use the &#8220;in&#8221; operator to check whether an item is present in the list. Similarly, we can also use the &#8220;not in&#8221; operator with the list.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n&gt;&gt;&gt; my_list = &#x5B;1, 2, 3]\n&gt;&gt;&gt; \n&gt;&gt;&gt; 1 in my_list\nTrue\n&gt;&gt;&gt; 10 in my_list\nFalse\n&gt;&gt;&gt; 10 not in my_list\nTrue\n&gt;&gt;&gt; 1 not in my_list\nFalse\n&gt;&gt;&gt; \n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Deleting a List<\/h2>\n\n\n\n<p>We can use the &#8220;del&#8221; keyword to delete a list index or the complete list itself.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n&gt;&gt;&gt; my_list = &#x5B;1, 2, 3]\n&gt;&gt;&gt; del my_list&#x5B;1]\n&gt;&gt;&gt; my_list\n&#x5B;1, 3]\n&gt;&gt;&gt; \n&gt;&gt;&gt; del my_list\n&gt;&gt;&gt; my_list\nTraceback (most recent call last):\n  File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;\nNameError: name &#039;my_list&#039; is not defined\n&gt;&gt;&gt; \n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Slicing a List<\/h2>\n\n\n\n<p>We can use slicing to create a new list from the elements of a list. This is useful in creating a new list from a source list. <\/p>\n\n\n\n<p>The slicing technique contains two indexes separated using a colon. The left index is included and the right index is excluded from the result.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nlist_numbers = &#x5B;1, 2, 3, 4, 5, 6, 7, 8]\n\nprint(list_numbers&#x5B;1:3])\nprint(list_numbers&#x5B;:4])\nprint(list_numbers&#x5B;5:])\nprint(list_numbers&#x5B;:-5])\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"752\" height=\"598\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/07\/python-list-slicing.png\" alt=\"Python List Slicing\" class=\"wp-image-520\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/07\/python-list-slicing.png 752w, https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/07\/python-list-slicing-300x239.png 300w\" sizes=\"auto, (max-width: 752px) 100vw, 752px\" \/><figcaption class=\"wp-element-caption\">List Slicing<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">List Concatenation (+ operator)<\/h2>\n\n\n\n<p>We can concatenate multiple lists of elements to create a new list using the + operator.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n&gt;&gt;&gt; l1 = &#x5B;1]\n&gt;&gt;&gt; l2 = &#x5B;2, 3]\n&gt;&gt;&gt; l3 = &#x5B;4, &quot;5&quot;, (6, 7)]\n&gt;&gt;&gt; \n&gt;&gt;&gt; l1 + l2 + l3\n&#x5B;1, 2, 3, 4, &#039;5&#039;, (6, 7)]\n&gt;&gt;&gt; \n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Repeating List Elements (* operator)<\/h2>\n\n\n\n<p>Python List also supports the * operator to create a new list with the elements repeated the specified number of times.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n&gt;&gt;&gt; l1 = &#x5B;1, 2]\n&gt;&gt;&gt; \n&gt;&gt;&gt; l1 * 3\n&#x5B;1, 2, 1, 2, 1, 2]\n&gt;&gt;&gt; \n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Python List Length<\/h2>\n\n\n\n<p>We can get the length or size of the list using the built-in len() function.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n&gt;&gt;&gt; list_numbers = &#x5B;1, 2, 3, 4]\n&gt;&gt;&gt; len(list_numbers)\n4\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">The built-in list() Constructor<\/h2>\n\n\n\n<p>We can create a list from iterable using the built-in list() constructor. This function accepts an iterable argument, so we can pass String, Tuple, etc.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n&gt;&gt;&gt; l1 = list(&quot;ABC&quot;)\n&gt;&gt;&gt; l1\n&#x5B;&#039;A&#039;, &#039;B&#039;, &#039;C&#039;]\n&gt;&gt;&gt; \n&gt;&gt;&gt; l1 = list((1, 2, 3))\n&gt;&gt;&gt; \n&gt;&gt;&gt; l1\n&#x5B;1, 2, 3]\n&gt;&gt;&gt; \n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Python List Functions<\/h2>\n\n\n\n<p>Let&#8217;s look at some of the functions present in the list object.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. append(object)<\/h3>\n\n\n\n<p>This function is used to append an element to the end of the list.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n&gt;&gt;&gt; list_numbers = &#x5B;1, 2, 3, 4]\n&gt;&gt;&gt; list_numbers.append(5)\n&gt;&gt;&gt; print(list_numbers)\n&#x5B;1, 2, 3, 4, 5]\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">2. index(object, start, end)<\/h3>\n\n\n\n<p>This function returns the first index of the object in the list. If the object is not found, then <code>ValueError<\/code> is raised. <\/p>\n\n\n\n<p>The start and end are optional arguments to specify the index from where to start and end the search of the object.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n&gt;&gt;&gt; list_numbers = &#x5B;1, 2, 1, 2, 1, 2]\n&gt;&gt;&gt; \n&gt;&gt;&gt; list_numbers.index(1)\n0\n&gt;&gt;&gt; list_numbers.index(1, 1)\n2\n&gt;&gt;&gt; list_numbers.index(1, 3, 10)\n4\n&gt;&gt;&gt; list_numbers.index(10)\nTraceback (most recent call last):\n  File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;\nValueError: 10 is not in list\n&gt;&gt;&gt; \n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">3. count(object)<\/h3>\n\n\n\n<p>This function returns the number of occurrences of the object in the list.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n&gt;&gt;&gt; list_numbers = &#x5B;1, 2, 1, 2, 1, 2]\n&gt;&gt;&gt; list_numbers.count(2)\n3\n&gt;&gt;&gt; list_numbers.count(1)\n3\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">4. reverse()<\/h3>\n\n\n\n<p>This function reverses the list elements.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n&gt;&gt;&gt; list_numbers = &#x5B;1, 2, 3]\n&gt;&gt;&gt; list_numbers.reverse()\n&gt;&gt;&gt; print(list_numbers)\n&#x5B;3, 2, 1]\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">5. clear()<\/h3>\n\n\n\n<p>This function removes all the elements from the list.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n&gt;&gt;&gt; list_numbers = &#x5B;1, 2, 5]\n&gt;&gt;&gt; list_numbers.clear()\n&gt;&gt;&gt; print(list_numbers)\n&#x5B;]\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">6. copy()<\/h3>\n\n\n\n<p>This function returns a shallow copy of the list.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n&gt;&gt;&gt; list_items = &#x5B;1, 2, 3]\n&gt;&gt;&gt; tmp_list = list_items.copy()\n&gt;&gt;&gt; print(tmp_list)\n&#x5B;1, 2, 3]\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">7. extend(iterable)<\/h3>\n\n\n\n<p>This function appends all the elements from the iterable to the end of this list. Some known iterable in Python are <a aria-label=\"Tuple (opens in a new tab)\" href=\"https:\/\/www.askpython.com\/python\/tuple\/python-tuple\" target=\"_blank\" rel=\"noreferrer noopener\">Tuple<\/a>, List, and String.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n&gt;&gt;&gt; list_num = &#x5B;]\n&gt;&gt;&gt; list_num.extend(&#x5B;1, 2])  # list iterable argument\n&gt;&gt;&gt; print(list_num)\n&#x5B;1, 2]\n&gt;&gt;&gt; list_num.extend((3, 4))  # tuple iterable argument\n&gt;&gt;&gt; print(list_num)\n&#x5B;1, 2, 3, 4]\n&gt;&gt;&gt; list_num.extend(&quot;ABC&quot;)  # string iterable argument\n&gt;&gt;&gt; print(list_num)\n&#x5B;1, 2, 3, 4, &#039;A&#039;, &#039;B&#039;, &#039;C&#039;]\n&gt;&gt;&gt; \n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">8. insert(index, object)<\/h3>\n\n\n\n<p>This method inserts the object just before the given index. <\/p>\n\n\n\n<p>If the index value is greater than the length of the list, the object is added at the end of the list. <\/p>\n\n\n\n<p>If the index value is negative and not in the range, then the object is added at the start of the list.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n&gt;&gt;&gt; my_list = &#x5B;1, 2, 3]\n&gt;&gt;&gt; \n&gt;&gt;&gt; my_list.insert(1, &#039;X&#039;)  # insert just before index 1\n&gt;&gt;&gt; print(my_list)\n&#x5B;1, &#039;X&#039;, 2, 3]\n&gt;&gt;&gt; \n&gt;&gt;&gt; my_list.insert(100, &#039;Y&#039;)  # insert at the end of the list\n&gt;&gt;&gt; print(my_list)\n&#x5B;1, &#039;X&#039;, 2, 3, &#039;Y&#039;]\n&gt;&gt;&gt; \n&gt;&gt;&gt; my_list.insert(-100, &#039;Z&#039;)  # negative and not in range, so insert at the start\n&gt;&gt;&gt; print(my_list)\n&#x5B;&#039;Z&#039;, 1, &#039;X&#039;, 2, 3, &#039;Y&#039;]\n&gt;&gt;&gt; my_list.insert(-2, &#039;A&#039;)  # negative and in the range, so insert before second last element\n&gt;&gt;&gt; print(my_list)\n&#x5B;&#039;Z&#039;, 1, &#039;X&#039;, 2, &#039;A&#039;, 3, &#039;Y&#039;]\n&gt;&gt;&gt; \n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">9. pop(index)<\/h3>\n\n\n\n<p>This function removes the element at the given index and returns it. If the index is not provided, the last element is removed and returned.<\/p>\n\n\n\n<p>This function raises <em>IndexError<\/em> if the list is empty or the index is out of range.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n&gt;&gt;&gt; my_list = &#x5B;1, 2, 3, 4]\n&gt;&gt;&gt; \n&gt;&gt;&gt; my_list.pop()\n4\n&gt;&gt;&gt; my_list\n&#x5B;1, 2, 3]\n&gt;&gt;&gt; my_list.pop(1)\n2\n&gt;&gt;&gt; my_list\n&#x5B;1, 3]\n&gt;&gt;&gt; my_list.pop(-1)\n3\n&gt;&gt;&gt; my_list\n&#x5B;1]\n&gt;&gt;&gt; my_list.pop(100)\nTraceback (most recent call last):\n  File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;\nIndexError: pop index out of range\n&gt;&gt;&gt; \n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">10. remove(object)<\/h3>\n\n\n\n<p>This function removes the first occurrence of the given object from the list. If the object is not found in the list, <em>ValueError<\/em> is raised.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n&gt;&gt;&gt; my_list = &#x5B;1,2,3,1,2,3]\n&gt;&gt;&gt; my_list.remove(2)\n&gt;&gt;&gt; my_list\n&#x5B;1, 3, 1, 2, 3]\n&gt;&gt;&gt; my_list.remove(20)\nTraceback (most recent call last):\n  File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;\nValueError: list.remove(x): x not in list\n&gt;&gt;&gt;\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">11. sort(key, reverse)<\/h3>\n\n\n\n<p>This function is used to sort the list elements. The list elements must implement <strong>__lt__(self, other)<\/strong> function.<\/p>\n\n\n\n<p>We can specify a <strong><a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/www.askpython.com\/python\/python-functions\" target=\"_blank\">function<\/a><\/strong> name as the <strong>key<\/strong> to be used for sorting. This way we can define our own custom function to use for sorting list elements.<\/p>\n\n\n\n<p>The reverse accepts a boolean value. If it&#8217;s <strong>True<\/strong>, then the list is sorted in the reverse order. The default value of reversed is <strong>False<\/strong> and the elements are sorted in the natural order.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n&gt;&gt;&gt; list_num = &#x5B;1, 0, 3, 4, -1, 5, 2]\n&gt;&gt;&gt; list_num.sort()\n&gt;&gt;&gt; list_num\n&#x5B;-1, 0, 1, 2, 3, 4, 5]\n&gt;&gt;&gt; list_num.sort(reverse=True)\n&gt;&gt;&gt; list_num\n&#x5B;5, 4, 3, 2, 1, 0, -1]\n&gt;&gt;&gt; \n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">List vs Tuple<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The list is a mutable sequence whereas Tuple is immutable.<\/li>\n\n\n\n<li>The list is preferred to store the same types of <a href=\"https:\/\/www.askpython.com\/python\/python-data-types\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\"data types (opens in a new tab)\">data types<\/a> where we need to add\/update them.<\/li>\n\n\n\n<li>A list requires more memory than Tuple because it supports dynamic length.<\/li>\n\n\n\n<li>Iterating over a list is slightly more time taking than a Tuple because its elements are not required to be stored in contiguous memory locations.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>In this tutorial, we learned that a Python List is a mutable sequence. It is an ordered collection of data, the data in a list can be accessed using its index value. The index value starts with 0 for the very first element in the sequence. The list also supports the negative index to refer to the elements from end to start.&nbsp;&nbsp;<\/p>\n\n\n\n<p>We have also covered Python&#8217;s different methods to add, insert, update, and remove elements from a list.&nbsp;Any iterable elements can transform into a list using the Python built-in list() constructor.<\/p>\n\n\n\n<p>Hope this tutorial helps you to understand the concept of List in Python.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">References:<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a rel=\"noreferrer noopener\" aria-label=\"Python List Data Structure (opens in a new tab)\" href=\"https:\/\/docs.python.org\/3\/tutorial\/datastructures.html#more-on-lists\" target=\"_blank\">Python List Data Structure<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/docs.python.org\/3\/library\/functions.html#func-list\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\"list() built-in function (opens in a new tab)\">list() built-in function<\/a><\/li>\n<\/ul>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python has four built-in data types data, set, tuple, dictionary, and list. These data types can store multiple collections of data. A set is an unordered collection of data, it is iterable, mutable, and has no duplicate elements. A tuple is an ordered collection of data but unlike a set, it is not mutable. A [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":39624,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-511","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-list"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/511","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=511"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/511\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/39624"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=511"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=511"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=511"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}