{"id":8765,"date":"2021-03-03T12:53:09","date_gmt":"2021-03-03T07:23:09","guid":{"rendered":"https:\/\/pynative.com\/?p=8765"},"modified":"2021-04-09T11:00:47","modified_gmt":"2021-04-09T05:30:47","slug":"python-lists","status":"publish","type":"post","link":"https:\/\/pynative.com\/python-lists\/","title":{"rendered":"Python Lists"},"content":{"rendered":"\n<p><strong>Python list is an ordered sequence of items<\/strong>.<\/p>\n\n\n\n<p>In this article you will learn the different methods of creating a list, adding, modifying, and deleting elements in the list. Also, learn how to iterate the list and access the elements in the list in detail. Nested Lists and List Comprehension are also discussed in detail with examples.<\/p>\n\n\n\n<div class=\"wp-block-columns sectionpy has-black-color has-text-color has-background is-layout-flex wp-container-core-columns-is-layout-9d6595d7 wp-block-columns-is-layout-flex\" style=\"background-color:#edf9f8\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<p><strong>Also See<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/pynative.com\/python-list-exercise-with-solutions\/\">Python List Exercise<\/a><\/li><li><a href=\"https:\/\/pynative.com\/python-list-quiz\/\">Python List Quiz<\/a><\/li><li><a href=\"https:\/\/pynative.com\/python-lists\/#h-summary-of-list-operations\">Summary of List Operations<\/a><\/li><\/ul>\n<\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"676\" height=\"395\" src=\"https:\/\/pynative.com\/wp-content\/uploads\/2021\/03\/python-list.jpg\" alt=\"Python List\" class=\"wp-image-8755\" srcset=\"https:\/\/pynative.com\/wp-content\/uploads\/2021\/03\/python-list.jpg 676w, https:\/\/pynative.com\/wp-content\/uploads\/2021\/03\/python-list-300x175.jpg 300w\" sizes=\"auto, (max-width: 676px) 100vw, 676px\" \/><figcaption>Python List<\/figcaption><\/figure><\/div>\n\n\n\n<p>The following are the <strong>properties of a list<\/strong>.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><b>Mutable: <\/b>The elements of the list can be modified. We can add or remove items to the list after it has been created.<\/li><li><strong>Ordered:<\/strong> The items in the lists are ordered. Each item has a unique index value. The new items will be added to the end of the list.<\/li><li><strong>Heterogenous<\/strong>: The list can contain different kinds of elements i.e; they can contain elements of string, integer, boolean, or any type.<\/li><li><strong>Duplicates:<\/strong> The list can contain duplicates i.e., lists can have two items with the same values.<\/li><\/ul>\n\n\n\n<p><strong>Why use a list?<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>The list data structure is very flexible It has many unique inbuilt functionalities like <code>pop()<\/code>, <code>append()<\/code>, etc which makes it easier, where the data keeps changing.<\/li><li>Also, the list can contain duplicate elements i.e two or more items can have the same values.<\/li><li>Lists are Heterogeneous i.e, different kinds of objects\/elements can be added<\/li><li>As Lists are mutable it is used in applications where the values of the items change frequently.<\/li><\/ul>\n\n\n\n<div class=\"wp-block-yoast-seo-table-of-contents yoast-table-of-contents\"><h2>Table of contents<\/h2><ul><li><a href=\"#h-creating-a-python-list\" data-level=\"2\">Creating a Python list<\/a><\/li><li><a href=\"#h-length-of-a-list\" data-level=\"2\">Length of a List<\/a><\/li><li><a href=\"#h-accessing-items-of-a-list\" data-level=\"2\">Accessing items of a List<\/a><ul><li><a href=\"#h-indexing\" data-level=\"3\">Indexing<\/a><\/li><li><a href=\"#h-negative-indexing\" data-level=\"3\">Negative Indexing<\/a><\/li><li><a href=\"#h-list-slicing\" data-level=\"3\">List Slicing<\/a><\/li><\/ul><\/li><li><a href=\"#h-iterating-a-list\" data-level=\"2\">Iterating a List<\/a><ul><li><a href=\"#h-iterate-along-with-an-index-number\" data-level=\"3\">Iterate along with an index number<\/a><\/li><\/ul><\/li><li><a href=\"#h-adding-elements-to-the-list\" data-level=\"2\">Adding elements to the list<\/a><ul><li><a href=\"#h-append-item-at-the-end-of-the-list\" data-level=\"3\">Append item at the end of the list<\/a><\/li><li><a href=\"#h-add-item-at-the-specified-position-in-the-list\" data-level=\"3\">Add item at the specified position in the list<\/a><\/li><li><a href=\"#h-using-extend\" data-level=\"3\">Using extend()<\/a><\/li><\/ul><\/li><li><a href=\"#h-modify-the-items-of-a-list\" data-level=\"2\">Modify the items of a List<\/a><ul><li><a href=\"#h-modify-all-items\" data-level=\"3\">Modify all items<\/a><\/li><\/ul><\/li><li><a href=\"#h-removing-elements-from-a-list\" data-level=\"2\">Removing elements from a List<\/a><ul><li><a href=\"#h-remove-specific-item\" data-level=\"3\">Remove specific item<\/a><ul><li><a href=\"#h-remove-all-occurrence-of-a-specific-item\" data-level=\"4\">Remove all occurrence of a specific item<\/a><\/li><\/ul><\/li><li><a href=\"#h-remove-item-present-at-given-index\" data-level=\"3\">Remove item present at given index<\/a><\/li><li><a href=\"#h-remove-the-range-of-items\" data-level=\"3\">Remove the range of items<\/a><\/li><li><a href=\"#h-remove-all-items\" data-level=\"3\">Remove all items<\/a><\/li><\/ul><\/li><li><a href=\"#h-finding-an-element-in-the-list\" data-level=\"2\">Finding an element in the list<\/a><\/li><li><a href=\"#h-concatenation-of-two-lists\" data-level=\"2\">Concatenation of two lists<\/a><\/li><li><a href=\"#h-copying-a-list\" data-level=\"2\">Copying a list<\/a><ul><li><a href=\"#h-using-assignment-operator\" data-level=\"3\">Using assignment operator (=)<\/a><\/li><li><a href=\"#h-using-the-copy-method\" data-level=\"3\">Using the copy() method<\/a><\/li><\/ul><\/li><li><a href=\"#h-list-operations\" data-level=\"2\">List operations<\/a><ul><li><a href=\"#h-sort-list-using-sort\" data-level=\"3\">Sort List using sort()<\/a><\/li><li><a href=\"#h-reverse-a-list-using-reverse\" data-level=\"3\">Reverse a List using reverse()<\/a><\/li><\/ul><\/li><li><a href=\"#h-python-built-in-functions-with-list\" data-level=\"2\">Python Built-in functions with List<\/a><ul><li><a href=\"#h-using-max-min\" data-level=\"3\">Using max() &amp; min()<\/a><\/li><li><a href=\"#h-using-sum\" data-level=\"3\">Using sum()<\/a><\/li><li><a href=\"#h-all\" data-level=\"3\">all() <\/a><\/li><li><a href=\"#h-any\" data-level=\"3\">any()<\/a><\/li><\/ul><\/li><li><a href=\"#h-nested-list\" data-level=\"2\">Nested List<\/a><\/li><li><a href=\"#h-list-comprehension\" data-level=\"2\">List Comprehension<\/a><\/li><li><a href=\"#h-summary-of-list-operations\" data-level=\"2\">Summary of List operations<\/a><\/li><\/ul><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-creating-a-python-list\">Creating a Python list<\/h2>\n\n\n\n<p>The list can be created using either the list constructor or using square brackets <code>[]<\/code>.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Using <code>list()<\/code> constructor: <\/strong>In general, the constructor of a class has its class name. Similarly, Create a list by passing the comma-separated values inside the <code>list()<\/code>.<\/li><li><strong>Using square bracket (<code>[]<\/code>)<\/strong>: In this method, we can create a list simply by enclosing the items inside the square brackets.<\/li><\/ul>\n\n\n\n<p>Let us see examples for creating the list using the above methods<\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code1\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-comment\"># Using list constructor<\/span>\nmy_list1 = list((<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">3<\/span>))\nprint(my_list1)\n<span class=\"hljs-comment\"># Output &#91;1, 2, 3]<\/span>\n\n<span class=\"hljs-comment\"># Using square brackets&#91;]<\/span>\nmy_list2 = &#91;<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">3<\/span>]\nprint(my_list2)\n<span class=\"hljs-comment\"># Output &#91;1, 2, 3]<\/span>\n\n<span class=\"hljs-comment\"># with heterogeneous items<\/span>\nmy_list3 = &#91;<span class=\"hljs-number\">1.0<\/span>, <span class=\"hljs-string\">'Jessa'<\/span>, <span class=\"hljs-number\">3<\/span>]\nprint(my_list3)\n<span class=\"hljs-comment\"># Output &#91;1.0, 'Jessa', 3]<\/span>\n\n<span class=\"hljs-comment\"># empty list using list()<\/span>\nmy_list4 = list()\nprint(my_list4)\n<span class=\"hljs-comment\"># Output &#91;]<\/span>\n\n<span class=\"hljs-comment\"># empty list using &#91;]<\/span>\nmy_list5 = &#91;]\nprint(my_list4)\n<span class=\"hljs-comment\"># Output &#91;]<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code1', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code1');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"h-length-of-a-list\"><strong>Length of a List<\/strong><\/h2>\n\n\n\n<p>In order to find the number of items present in a list, we can use the <code>len()<\/code> function.<\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code2\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">my_list = &#91;<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">3<\/span>]\nprint(len(my_list))\n<span class=\"hljs-comment\"># output 3<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code2', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code2');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"h-accessing-items-of-a-list\">Accessing items of a List<\/h2>\n\n\n\n<p>The items in a list can be accessed through indexing and slicing. This section will guide you by accessing the list using the following two ways<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Using indexing<\/strong>, we can access any item from a list using its index number<\/li><li><strong>Using slicing<\/strong>, we can access a range of items from a list<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-indexing\">Indexing<\/h3>\n\n\n\n<p>The list elements can be accessed using the &#8220;indexing&#8221; technique. Lists are ordered collections with unique indexes for each item. We can access the items in the list using this index number.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"745\" height=\"231\" src=\"https:\/\/pynative.com\/wp-content\/uploads\/2021\/02\/positive-and-negative-indexing.jpg\" alt=\"Python positive and negative indexing\" class=\"wp-image-8185\" srcset=\"https:\/\/pynative.com\/wp-content\/uploads\/2021\/02\/positive-and-negative-indexing.jpg 745w, https:\/\/pynative.com\/wp-content\/uploads\/2021\/02\/positive-and-negative-indexing-300x93.jpg 300w\" sizes=\"auto, (max-width: 745px) 100vw, 745px\" \/><figcaption>Python Positive and Negative indexing<\/figcaption><\/figure><\/div>\n\n\n\n<p>To access the elements in the list from left to right, the index value starts from <strong>zero<\/strong> to <strong>(length of the list-1)<\/strong> can be used. For example, if we want to access the 3rd element we need to use 2 since the index value starts from 0.<\/p>\n\n\n\n<p><strong>Note<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>As Lists are ordered sequences of items, the index values start from 0 to the Lists length.<\/li><li>Whenever we try to access an item with an index more than the Lists length, it will throw the <code>'Index Error'<\/code>.<\/li><li>Similarly, the index values are always an integer. If we give any other type, then it will throw <code>Type Error<\/code>.<\/li><\/ul>\n\n\n\n<p>Example<\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code3\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">my_list = &#91;<span class=\"hljs-number\">10<\/span>, <span class=\"hljs-number\">20<\/span>, <span class=\"hljs-string\">'Jessa'<\/span>, <span class=\"hljs-number\">12.50<\/span>, <span class=\"hljs-string\">'Emma'<\/span>]\n<span class=\"hljs-comment\"># accessing 2nd element of the list<\/span>\nprint(my_list&#91;<span class=\"hljs-number\">1<\/span>])  <span class=\"hljs-comment\"># 20<\/span>\n<span class=\"hljs-comment\"># accessing 5th element of the list<\/span>\nprint(my_list&#91;<span class=\"hljs-number\">4<\/span>])  <span class=\"hljs-comment\"># 'Emma'<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code3', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code3');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p>As seen in the above example we accessed the second element in the list by passing the index value as 1. Similarly, we passed index 4 to access the 5th element in the list.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-negative-indexing\">Negative Indexing<\/h3>\n\n\n\n<p>The elements in the list can be accessed from right to left by using <strong>negative indexing<\/strong>. The negative value starts from -1 to -length of the list. It indicates that the list is indexed from the reverse\/backward.<\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code4\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">my_list = &#91;<span class=\"hljs-number\">10<\/span>, <span class=\"hljs-number\">20<\/span>, <span class=\"hljs-string\">'Jessa'<\/span>, <span class=\"hljs-number\">12.50<\/span>, <span class=\"hljs-string\">'Emma'<\/span>]\n<span class=\"hljs-comment\"># accessing last element of the list<\/span>\nprint(my_list&#91;<span class=\"hljs-number\">-1<\/span>])\n<span class=\"hljs-comment\"># output 'Emma'<\/span>\n\n<span class=\"hljs-comment\"># accessing second last element of the list<\/span>\nprint(my_list&#91;<span class=\"hljs-number\">-2<\/span>])\n<span class=\"hljs-comment\"># output 12.5<\/span>\n\n<span class=\"hljs-comment\"># accessing 4th element from last<\/span>\nprint(my_list&#91;<span class=\"hljs-number\">-4<\/span>])\n<span class=\"hljs-comment\"># output 20<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code4', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code4');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p>As seen in the above example to access the 4th element from the last (right to left) we pass &#8216;-4&#8217; in the index value.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-list-slicing\">List Slicing<\/h3>\n\n\n\n<p>Slicing a list implies, accessing a range of elements in a list. For example, if we want to get the elements in the position from 3 to 7, we can use the slicing method. We can even modify the values in a range by using this slicing technique.<\/p>\n\n\n\n<p>The below is the syntax for list slicing.<\/p>\n\n\n<pre id=\"syntax\" class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">listname&#91;start_index : end_index : step]<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<ul class=\"wp-block-list\"><li>The&nbsp;<code>start_index<\/code> denotes the index position from where the slicing should begin and the <code>end_index<\/code>&nbsp;parameter denotes the index positions till which the slicing should be done.<\/li><li>The <code>step<\/code> allows you to take each nth-element within a <code>start_index:end_index<\/code> range.<\/li><\/ul>\n\n\n\n<p><strong>Example<\/strong><\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code5\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">my_list = &#91;<span class=\"hljs-number\">10<\/span>, <span class=\"hljs-number\">20<\/span>, <span class=\"hljs-string\">'Jessa'<\/span>, <span class=\"hljs-number\">12.50<\/span>, <span class=\"hljs-string\">'Emma'<\/span>, <span class=\"hljs-number\">25<\/span>, <span class=\"hljs-number\">50<\/span>]\n<span class=\"hljs-comment\"># Extracting a portion of the list from 2nd till 5th element<\/span>\nprint(my_list&#91;<span class=\"hljs-number\">2<\/span>:<span class=\"hljs-number\">5<\/span>])\n<span class=\"hljs-comment\"># Output &#91;'Jessa', 12.5, 'Emma']<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code5', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code5');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p>Let us see few more examples of slicing a list such as<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Extract a portion of the list<\/li><li>Reverse a list<\/li><li>Slicing with a step<\/li><li>Slice without specifying start or end position<\/li><\/ul>\n\n\n\n<p><strong>Example<\/strong><\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code6\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">my_list = &#91;<span class=\"hljs-number\">5<\/span>, <span class=\"hljs-number\">8<\/span>, <span class=\"hljs-string\">'Tom'<\/span>, <span class=\"hljs-number\">7.50<\/span>, <span class=\"hljs-string\">'Emma'<\/span>]\n\n<span class=\"hljs-comment\"># slice first four items<\/span>\nprint(my_list&#91;:<span class=\"hljs-number\">4<\/span>])\n<span class=\"hljs-comment\"># Output &#91;5, 8, 'Tom', 7.5]<\/span>\n\n<span class=\"hljs-comment\"># print every second element<\/span>\n<span class=\"hljs-comment\"># with a skip count 2<\/span>\nprint(my_list&#91;::<span class=\"hljs-number\">2<\/span>])\n<span class=\"hljs-comment\"># Output &#91;5, 'Tom', 'Emma']<\/span>\n\n<span class=\"hljs-comment\"># reversing the list<\/span>\nprint(my_list&#91;::<span class=\"hljs-number\">-1<\/span>])\n<span class=\"hljs-comment\"># Output &#91;'Emma', 7.5, 'Tom', 8, 5]<\/span>\n\n<span class=\"hljs-comment\"># Without end_value<\/span>\n<span class=\"hljs-comment\"># Stating from 3nd item to last item<\/span>\nprint(my_list&#91;<span class=\"hljs-number\">3<\/span>:])\n<span class=\"hljs-comment\"># Output &#91;7.5, 'Emma']<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code6', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code6');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"h-iterating-a-list\">Iterating<strong> a List<\/strong><\/h2>\n\n\n\n<p>The objects in the list can be iterated over one by one, by using a for a loop.<\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code7\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">my_list = &#91;<span class=\"hljs-number\">5<\/span>, <span class=\"hljs-number\">8<\/span>, <span class=\"hljs-string\">'Tom'<\/span>, <span class=\"hljs-number\">7.50<\/span>, <span class=\"hljs-string\">'Emma'<\/span>]\n\n<span class=\"hljs-comment\"># iterate a list<\/span>\n<span class=\"hljs-keyword\">for<\/span> item <span class=\"hljs-keyword\">in<\/span> my_list:\n    print(item)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code7', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code7');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">5\n8\nTom\n7.5\nEmma<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-iterate-along-with-an-index-number\">Iterate along with an index number<\/h3>\n\n\n\n<p>The index value starts from 0 to (length of the list-1). Hence using the function range() is ideal for this scenario.<\/p>\n\n\n\n<p>The range function returns a sequence of numbers. By default, it returns starting from 0 to the specified number (increments by 1). The starting and ending values can be passed according to our needs.<\/p>\n\n\n\n<p><strong>Example<\/strong><\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code8\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">my_list = &#91;<span class=\"hljs-number\">5<\/span>, <span class=\"hljs-number\">8<\/span>, <span class=\"hljs-string\">'Tom'<\/span>, <span class=\"hljs-number\">7.50<\/span>, <span class=\"hljs-string\">'Emma'<\/span>]\n\n<span class=\"hljs-comment\"># iterate a list<\/span>\n<span class=\"hljs-keyword\">for<\/span> i <span class=\"hljs-keyword\">in<\/span> range(<span class=\"hljs-number\">0<\/span>, len(my_list)):\n    <span class=\"hljs-comment\"># print each item using index number<\/span>\n    print(my_list&#91;i])<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code8', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code8');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">5\n8\nTom\n7.5\nEmma<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-adding-elements-to-the-list\"><strong>Adding elements to the list<\/strong><\/h2>\n\n\n\n<p>We can add a new element\/list of elements to the list using the list methods such as <code>append()<\/code>, <code>insert()<\/code>, and <code>extend()<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-append-item-at-the-end-of-the-list\">Append item at the end of the list<\/h3>\n\n\n\n<p>The append() method will accept only one parameter and add it at the end of the list.<\/p>\n\n\n\n<p>Let&#8217;s see the example to add the element &#8216;Emma&#8217; at the end of the list.<\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code9\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">my_list = list(&#91;<span class=\"hljs-number\">5<\/span>, <span class=\"hljs-number\">8<\/span>, <span class=\"hljs-string\">'Tom'<\/span>, <span class=\"hljs-number\">7.50<\/span>])\n\n<span class=\"hljs-comment\"># Using append()<\/span>\nmy_list.append(<span class=\"hljs-string\">'Emma'<\/span>)\nprint(my_list)\n<span class=\"hljs-comment\"># Output &#91;5, 8, 'Tom', 7.5, 'Emma']<\/span>\n\n<span class=\"hljs-comment\"># append the nested list at the end<\/span>\nmy_list.append(&#91;<span class=\"hljs-number\">25<\/span>, <span class=\"hljs-number\">50<\/span>, <span class=\"hljs-number\">75<\/span>])\nprint(my_list)\n<span class=\"hljs-comment\"># Output &#91;5, 8, 'Tom', 7.5, 'Emma', &#91;25, 50, 75]]<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code9', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code9');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"h-add-item-at-the-specified-position-in-the-list\">Add item at the specified position in the list<\/h3>\n\n\n\n<p>Use the <code>insert()<\/code> method to add the object\/item at the specified position in the list. The insert method accepts two parameters position and object.<\/p>\n\n\n<pre id=\"syntax\" class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">insert(index, object)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>It will insert the <code>object<\/code> in the specified <code>index<\/code>. Let us see this with an example.<\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code10\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">my_list = list(&#91;<span class=\"hljs-number\">5<\/span>, <span class=\"hljs-number\">8<\/span>, <span class=\"hljs-string\">'Tom'<\/span>, <span class=\"hljs-number\">7.50<\/span>])\n\n<span class=\"hljs-comment\"># Using insert()<\/span>\n<span class=\"hljs-comment\"># insert 25 at position 2<\/span>\nmy_list.insert(<span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">25<\/span>)\nprint(my_list)\n<span class=\"hljs-comment\"># Output &#91;5, 8, 25, 'Tom', 7.5]<\/span>\n\n<span class=\"hljs-comment\"># insert nested list at at position 3<\/span>\nmy_list.insert(<span class=\"hljs-number\">3<\/span>, &#91;<span class=\"hljs-number\">25<\/span>, <span class=\"hljs-number\">50<\/span>, <span class=\"hljs-number\">75<\/span>])\nprint(my_list)\n<span class=\"hljs-comment\"># Output &#91;5, 8, 25, &#91;25, 50, 75], 'Tom', 7.5]<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code10', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code10');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p>As seen in the above example item 25 is added at the index position 2.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-using-extend\"><strong>Using extend()<\/strong><\/h3>\n\n\n\n<p>The extend method will accept the list of elements and add them at the end of the list. We can even add another list by using this method.<\/p>\n\n\n\n<p>Let&#8217;s add three items at the end of the list.<\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code11\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">my_list = list(&#91;<span class=\"hljs-number\">5<\/span>, <span class=\"hljs-number\">8<\/span>, <span class=\"hljs-string\">'Tom'<\/span>, <span class=\"hljs-number\">7.50<\/span>])\n\n<span class=\"hljs-comment\"># Using extend()<\/span>\nmy_list.extend(&#91;<span class=\"hljs-number\">25<\/span>, <span class=\"hljs-number\">75<\/span>, <span class=\"hljs-number\">100<\/span>])\nprint(my_list)\n<span class=\"hljs-comment\"># Output &#91;5, 8, 'Tom', 7.5, 25, 75, 100]<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code11', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code11');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p>As seen in the above example we have three integer values at once. All the values get added in the order they were passed and it gets appended at the end of the list.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-modify-the-items-of-a-list\">Modify the items of a List<\/h2>\n\n\n\n<p>The list is a mutable sequence of iterable objects. It means we can modify the items of a list.&nbsp;Use the index number and assignment operator (<code>=<\/code>) to assign a new value to an item.<\/p>\n\n\n\n<p>Let&#8217;s see how to perform the following two modification scenarios<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Modify the individual item.<\/li><li>Modify the range of items<\/li><\/ul>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code12\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-14\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">my_list = list(&#91;<span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">6<\/span>, <span class=\"hljs-number\">8<\/span>, <span class=\"hljs-number\">10<\/span>, <span class=\"hljs-number\">12<\/span>])\n\n<span class=\"hljs-comment\"># modify single item<\/span>\nmy_list&#91;<span class=\"hljs-number\">0<\/span>] = <span class=\"hljs-number\">20<\/span>\nprint(my_list)\n<span class=\"hljs-comment\"># Output &#91;20, 4, 6, 8, 10, 12]<\/span>\n\n<span class=\"hljs-comment\"># modify range of items<\/span>\n<span class=\"hljs-comment\"># modify from 1st index to 4th<\/span>\nmy_list&#91;<span class=\"hljs-number\">1<\/span>:<span class=\"hljs-number\">4<\/span>] = &#91;<span class=\"hljs-number\">40<\/span>, <span class=\"hljs-number\">60<\/span>, <span class=\"hljs-number\">80<\/span>]\nprint(my_list)\n<span class=\"hljs-comment\"># Output &#91;20, 40, 60, 80, 10, 12]<\/span>\n\n<span class=\"hljs-comment\"># modify from 3rd index to end<\/span>\nmy_list&#91;<span class=\"hljs-number\">3<\/span>:] = &#91;<span class=\"hljs-number\">80<\/span>, <span class=\"hljs-number\">100<\/span>, <span class=\"hljs-number\">120<\/span>]\nprint(my_list)\n<span class=\"hljs-comment\"># Output &#91;20, 40, 60, 80, 100, 120]<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-14\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code12', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code12');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"h-modify-all-items\"><strong>Modify all items<\/strong><\/h3>\n\n\n\n<p>Use for loop to iterate and modify all items at once. Let&#8217;s see how to modify each item of a list.<\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code13\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-15\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">my_list = list(&#91;<span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">6<\/span>, <span class=\"hljs-number\">8<\/span>])\n\n<span class=\"hljs-comment\"># change value of all items<\/span>\n<span class=\"hljs-keyword\">for<\/span> i <span class=\"hljs-keyword\">in<\/span> range(len(my_list)):\n    <span class=\"hljs-comment\"># calculate square of each number<\/span>\n    square = my_list&#91;i] * my_list&#91;i]\n    my_list&#91;i] = square\n\nprint(my_list)\n<span class=\"hljs-comment\"># Output &#91;4, 16, 36, 64]<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-15\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code13', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code13');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"h-removing-elements-from-a-list\">Removing elements from a List<\/h2>\n\n\n\n<p>The elements from the list can be removed using the following list methods.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th><strong>method<\/strong><\/th><th>Description<\/th><\/tr><\/thead><tbody><tr><td><code>remove(item)<\/code><\/td><td>To remove the first occurrence of the item from the list.<\/td><\/tr><tr><td><code>pop(index)<\/code><\/td><td>Removes and returns the item at the given index from the list.<\/td><\/tr><tr><td><code>clear()<\/code><\/td><td>To remove all items from the list. The output will be an empty list.<\/td><\/tr><tr><td><code>del list_name<\/code><\/td><td>Delete the entire list.<\/td><\/tr><\/tbody><\/table><figcaption>Python List methods to remove item<\/figcaption><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-remove-specific-item\">Remove specific item<\/h3>\n\n\n\n<p>Use the <code>remove()<\/code> method to remove the first occurrence of the item from the list.<\/p>\n\n\n\n<p><strong>Note<\/strong>:&nbsp; It Throws a <code>keyerror<\/code> if an item not present in the original list.<\/p>\n\n\n\n<p><strong>Example<\/strong><\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code14\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-16\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">my_list = list(&#91;<span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">6<\/span>, <span class=\"hljs-number\">8<\/span>, <span class=\"hljs-number\">10<\/span>, <span class=\"hljs-number\">12<\/span>])\n\n<span class=\"hljs-comment\"># remove item 6<\/span>\nmy_list.remove(<span class=\"hljs-number\">6<\/span>)\n<span class=\"hljs-comment\"># remove item 8<\/span>\nmy_list.remove(<span class=\"hljs-number\">8<\/span>)\n\nprint(my_list)\n<span class=\"hljs-comment\"># Output &#91;2, 4, 10, 12]<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-16\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code14', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code14');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<h4 class=\"wp-block-heading\" id=\"h-remove-all-occurrence-of-a-specific-item\">Remove all occurrence of a specific item<\/h4>\n\n\n\n<p>Use a loop to remove all occurrence of a specific item<\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code15\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-17\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">my_list = list(&#91;<span class=\"hljs-number\">6<\/span>, <span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">6<\/span>, <span class=\"hljs-number\">6<\/span>, <span class=\"hljs-number\">8<\/span>, <span class=\"hljs-number\">12<\/span>])\n\n<span class=\"hljs-keyword\">for<\/span> item <span class=\"hljs-keyword\">in<\/span> my_list:\n    my_list.remove(<span class=\"hljs-number\">6<\/span>)\n\nprint(my_list)\n<span class=\"hljs-comment\"># Output &#91;4, 8, 12]<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-17\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code15', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code15');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"h-remove-item-present-at-given-index\">Remove item present at given index<\/h3>\n\n\n\n<p>Use the <code>pop()<\/code> method to remove the item at the given index. The <code>pop()<\/code> method removes and returns the item present at the given index.<\/p>\n\n\n\n<p><strong>Note<\/strong>: It will remove the last time from the list if the index number is not passed.<\/p>\n\n\n\n<p><strong>Example<\/strong><\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code16\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-18\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">my_list = list(&#91;<span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">6<\/span>, <span class=\"hljs-number\">8<\/span>, <span class=\"hljs-number\">10<\/span>, <span class=\"hljs-number\">12<\/span>])\n\n<span class=\"hljs-comment\"># remove item present at index 2<\/span>\nmy_list.pop(<span class=\"hljs-number\">2<\/span>)\nprint(my_list)\n<span class=\"hljs-comment\"># Output &#91;2, 4, 8, 10, 12]<\/span>\n\n<span class=\"hljs-comment\"># remove item without passing index number<\/span>\nmy_list.pop()\nprint(my_list)\n<span class=\"hljs-comment\"># Output &#91;2, 4, 8, 10]<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-18\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code16', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code16');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"h-remove-the-range-of-items\">Remove the range of items<\/h3>\n\n\n\n<p>Use del keyword along with list slicing to remove the range of items<\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code17\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-19\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">my_list = list(&#91;<span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">6<\/span>, <span class=\"hljs-number\">8<\/span>, <span class=\"hljs-number\">10<\/span>, <span class=\"hljs-number\">12<\/span>])\n\n<span class=\"hljs-comment\"># remove range of items<\/span>\n<span class=\"hljs-comment\"># remove item from index 2 to 5<\/span>\n<span class=\"hljs-keyword\">del<\/span> my_list&#91;<span class=\"hljs-number\">2<\/span>:<span class=\"hljs-number\">5<\/span>]\nprint(my_list)\n<span class=\"hljs-comment\"># Output &#91;2, 4, 12]<\/span>\n\n<span class=\"hljs-comment\"># remove all items starting from index 3<\/span>\nmy_list = list(&#91;<span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">6<\/span>, <span class=\"hljs-number\">8<\/span>, <span class=\"hljs-number\">10<\/span>, <span class=\"hljs-number\">12<\/span>])\n<span class=\"hljs-keyword\">del<\/span> my_list&#91;<span class=\"hljs-number\">3<\/span>:]\nprint(my_list)\n<span class=\"hljs-comment\"># Output &#91;2, 4, 6]<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-19\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code17', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code17');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"h-remove-all-items\">Remove all items<\/h3>\n\n\n\n<p>Use the list&#8217; <code>clear()<\/code> method to remove all items from the list. The <code>clear()<\/code> method truncates the list.<\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code18\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-20\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">my_list = list(&#91;<span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">6<\/span>, <span class=\"hljs-number\">8<\/span>, <span class=\"hljs-number\">10<\/span>, <span class=\"hljs-number\">12<\/span>])\n\n<span class=\"hljs-comment\"># clear list<\/span>\nmy_list.clear()\nprint(my_list)\n<span class=\"hljs-comment\"># Output &#91;]<\/span>\n\n<span class=\"hljs-comment\"># Delete entire list<\/span>\n<span class=\"hljs-keyword\">del<\/span> my_list<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-20\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code18', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code18');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"h-finding-an-element-in-the-list\">Finding an element in the list<\/h2>\n\n\n\n<p>Use the <code>index()<\/code> function to find an item in a list.<\/p>\n\n\n\n<p>The <code>index()<\/code> function will accept the value of the element as a parameter and returns the first occurrence of the element or returns <code>ValueError<\/code> if the element does not exist.<\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code19\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-21\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">my_list = list(&#91;<span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">6<\/span>, <span class=\"hljs-number\">8<\/span>, <span class=\"hljs-number\">10<\/span>, <span class=\"hljs-number\">12<\/span>])\n\nprint(my_list.index(<span class=\"hljs-number\">8<\/span>))\n<span class=\"hljs-comment\"># Output 3<\/span>\n\n<span class=\"hljs-comment\"># returns error since the element does not exist in the list.<\/span>\n<span class=\"hljs-comment\"># my_list.index(100)<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-21\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code19', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code19');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"h-concatenation-of-two-lists\">Concatenation of two lists<\/h2>\n\n\n\n<p>The concatenation of two lists means merging of two lists. There are two ways to do that.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Using the <code>+<\/code> operator.<\/li><li>Using the <code>extend()<\/code> method. The <code>extend()<\/code> method appends the new list&#8217;s items at the end of the calling list.<\/li><\/ul>\n\n\n\n<p><strong>Example<\/strong><\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code20\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-22\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">my_list1 = &#91;<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">3<\/span>]\nmy_list2 = &#91;<span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">5<\/span>, <span class=\"hljs-number\">6<\/span>]\n\n<span class=\"hljs-comment\"># Using + operator<\/span>\nmy_list3 = my_list1 + my_list2\nprint(my_list3)\n<span class=\"hljs-comment\"># Output &#91;1, 2, 3, 4, 5, 6]<\/span>\n\n<span class=\"hljs-comment\"># Using extend() method<\/span>\nmy_list1.extend(my_list2)\nprint(my_list1)\n<span class=\"hljs-comment\"># Output &#91;1, 2, 3, 4, 5, 6]<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-22\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code20', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code20');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"h-copying-a-list\"><strong>Copying a list<\/strong><\/h2>\n\n\n\n<p>There are two ways by which a copy of a list can be created. Let us see each one with an example.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-using-assignment-operator\">Using assignment operator (<code>=<\/code>)<\/h3>\n\n\n\n<p>This is a straightforward way of creating a copy. In this method, the new list will be a deep copy. The changes that we make in the original list will be reflected in the new list.<\/p>\n\n\n\n<p>This is called <strong>deep copying.<\/strong><\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code21\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-23\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">my_list1 = &#91;<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">3<\/span>]\n\n<span class=\"hljs-comment\"># Using = operator<\/span>\nnew_list = my_list1\n<span class=\"hljs-comment\"># printing the new list<\/span>\nprint(new_list)\n<span class=\"hljs-comment\"># Output &#91;1, 2, 3]<\/span>\n\n<span class=\"hljs-comment\"># making changes in the original list<\/span>\nmy_list1.append(<span class=\"hljs-number\">4<\/span>)\n\n<span class=\"hljs-comment\"># print both copies<\/span>\nprint(my_list1)\n<span class=\"hljs-comment\"># result &#91;1, 2, 3, 4]<\/span>\nprint(new_list)\n<span class=\"hljs-comment\"># result &#91;1, 2, 3, 4]<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-23\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code21', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code21');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p>As seen in the above example a copy of the list has been created. The changes made to the original list are reflected in the copied list as well.<\/p>\n\n\n\n<p>Note: When you set <code>list1 = list2<\/code>, you are making them refer to the same <code>list<\/code> object, so when you modify one of them, all references associated with that object reflect the current state of the object. So don\u2019t use the assignment operator to copy the dictionary instead use the <code>copy()<\/code>&nbsp;method.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-using-the-copy-method\">Using the copy() method<\/h3>\n\n\n\n<p>The copy method can be used to create a copy of a list. This will create a new list and any changes made in the original list will not reflect in the new list. This is <strong>shallow copying<\/strong>.<\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code22\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-24\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">my_list1 = &#91;<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">3<\/span>]\n\n<span class=\"hljs-comment\"># Using copy() method<\/span>\nnew_list = my_list1.copy()\n<span class=\"hljs-comment\"># printing the new list<\/span>\nprint(new_list)\n<span class=\"hljs-comment\"># Output &#91;1, 2, 3]<\/span>\n\n<span class=\"hljs-comment\"># making changes in the original list<\/span>\nmy_list1.append(<span class=\"hljs-number\">4<\/span>)\n\n<span class=\"hljs-comment\"># print both copies<\/span>\nprint(my_list1)\n<span class=\"hljs-comment\"># result &#91;1, 2, 3, 4]<\/span>\nprint(new_list)\n<span class=\"hljs-comment\"># result &#91;1, 2, 3]<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-24\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code22', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code22');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p>As seen in the above example a copy of the list has been created. The changes made to the original list are not reflected in the copy.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-list-operations\">List operations<\/h2>\n\n\n\n<p>We can perform some operations over the list by using certain functions like <code>sort()<\/code>, <code>reverse()<\/code>, <code>clear()<\/code> etc.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-sort-list-using-sort\">Sort List using sort()<\/h3>\n\n\n\n<p>The sort function sorts the elements in the list in ascending order.<\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code23\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-25\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">mylist = &#91;<span class=\"hljs-number\">3<\/span>,<span class=\"hljs-number\">2<\/span>,<span class=\"hljs-number\">1<\/span>]\nmylist.sort()\nprint(mylist)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-25\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code23', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code23');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[1, 2, 3]<\/pre>\n\n\n\n<p>As seen in the above example the items are sorted in the ascending order.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-reverse-a-list-using-reverse\">Reverse a List using reverse()<\/h3>\n\n\n\n<p>The reverse function is used to reverse the elements in the list.<\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code24\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-26\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">mylist = &#91;<span class=\"hljs-number\">3<\/span>, <span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">5<\/span>, <span class=\"hljs-number\">6<\/span>, <span class=\"hljs-number\">1<\/span>]\nmylist.reverse()\nprint(mylist)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-26\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code24', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code24');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[1, 6, 5, 4, 3]<\/pre>\n\n\n\n<p>As seen in the above example the items in the list are printed in the reverse order here.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-python-built-in-functions-with-list\">Python Built-in functions with List<\/h2>\n\n\n\n<p>In addition to the built-in methods available in the list, we can use the built-in functions as well on the list. Let us see a few of them for example.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-using-max-min\">Using max() &amp; min()<\/h3>\n\n\n\n<p>The max function returns the maximum value in the list while the min function returns the minimum value in the list.<\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code25\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-27\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">mylist = &#91;<span class=\"hljs-number\">3<\/span>, <span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">5<\/span>, <span class=\"hljs-number\">6<\/span>, <span class=\"hljs-number\">1<\/span>]\nprint(max(mylist)) <span class=\"hljs-comment\">#returns the maximum number in the list.<\/span>\nprint(min(mylist)) <span class=\"hljs-comment\">#returns the minimum number in the list.<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-27\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code25', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code25');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">6\n1<\/pre>\n\n\n\n<p>As seen in the above example the <code>max<\/code>&nbsp;function returns 6 and <code>min<\/code> function returns 1.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-using-sum\">Using sum()<\/h3>\n\n\n\n<p>The sum function returns the sum of all the elements in the list.<\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code26\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-28\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">mylist = &#91;<span class=\"hljs-number\">3<\/span>, <span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">5<\/span>, <span class=\"hljs-number\">6<\/span>, <span class=\"hljs-number\">1<\/span>]\nprint(sum(mylist)) <\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-28\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code26', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code26');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">19\n<\/pre>\n\n\n\n<p>As seen in the above example the sum function returns the sum of all the elements in the list.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-all\">all() <\/h3>\n\n\n\n<p>In the case of all() function, the return value will be true only when all the values inside the list are true. Let us see the different item values and the return values.<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-regular\"><table><thead><tr><th>Item Values in List<\/th><th>Return Value<\/th><\/tr><\/thead><tbody><tr><td>All Values are True<\/td><td>True<\/td><\/tr><tr><td>One or more False Values<\/td><td>False<\/td><\/tr><tr><td>All False Values<\/td><td>False<\/td><\/tr><tr><td>Empty List<\/td><td>True<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code27\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-29\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-comment\">#with all true values<\/span>\nsamplelist1 = &#91;<span class=\"hljs-number\">1<\/span>,<span class=\"hljs-number\">1<\/span>,<span class=\"hljs-literal\">True<\/span>]\nprint(<span class=\"hljs-string\">\"all() All True values::\"<\/span>,all(samplelist1))\n\n<span class=\"hljs-comment\">#with one false<\/span>\nsamplelist2 = &#91;<span class=\"hljs-number\">0<\/span>,<span class=\"hljs-number\">1<\/span>,<span class=\"hljs-literal\">True<\/span>,<span class=\"hljs-number\">1<\/span>]\nprint(<span class=\"hljs-string\">\"all() with One false value ::\"<\/span>,all(samplelist2))\n\n<span class=\"hljs-comment\">#with all false<\/span>\nsamplelist3 = &#91;<span class=\"hljs-number\">0<\/span>,<span class=\"hljs-number\">0<\/span>,<span class=\"hljs-literal\">False<\/span>]\nprint(<span class=\"hljs-string\">\"all() with all false values ::\"<\/span>,all(samplelist3))\n\n<span class=\"hljs-comment\">#empty list<\/span>\nsamplelist4 = &#91;]<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-29\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code27', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code27');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">all() All True values:: True\nall() with One false value :: False\nall() with all false values :: False\nall() Empty list :: True<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-any\">any()<\/h3>\n\n\n\n<p>The any() method will return true if there is at least one true value. In the case of Empty List, it will return false.<br>Let us see the same possible combination of values for any() function in a list and its return values.<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-regular\"><table><thead><tr><th>Item Values in List<\/th><th>Return Value<\/th><\/tr><\/thead><tbody><tr><td>All Values are True<\/td><td>True<\/td><\/tr><tr><td>One or more False Values<\/td><td>True<\/td><\/tr><tr><td>All False Values<\/td><td>False<\/td><\/tr><tr><td>Empty List<\/td><td>False<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Similarly, let&#8217;s see each one of the above scenarios with a small example.<\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code28\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-30\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-comment\">#with all true values<\/span>\nsamplelist1 = &#91;<span class=\"hljs-number\">1<\/span>,<span class=\"hljs-number\">1<\/span>,<span class=\"hljs-literal\">True<\/span>]\nprint(<span class=\"hljs-string\">\"any() True values::\"<\/span>,any(samplelist1))\n\n<span class=\"hljs-comment\">#with one false<\/span>\nsamplelist2 = &#91;<span class=\"hljs-number\">0<\/span>,<span class=\"hljs-number\">1<\/span>,<span class=\"hljs-literal\">True<\/span>,<span class=\"hljs-number\">1<\/span>]\nprint(<span class=\"hljs-string\">\"any() One false value ::\"<\/span>,any(samplelist2))\n\n\n<span class=\"hljs-comment\">#with all false<\/span>\nsamplelist3 = &#91;<span class=\"hljs-number\">0<\/span>,<span class=\"hljs-number\">0<\/span>,<span class=\"hljs-literal\">False<\/span>]\nprint(<span class=\"hljs-string\">\"any() all false values ::\"<\/span>,any(samplelist3))\n\n<span class=\"hljs-comment\">#empty list<\/span>\nsamplelist4 = &#91;]\nprint(<span class=\"hljs-string\">\"any() Empty list ::\"<\/span>,any(samplelist4))<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-30\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code28', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code28');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">any() True values:: True\nany() One false value :: True\nany() all false values :: False\nany() Empty list :: False<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-nested-list\">Nested List<\/h2>\n\n\n\n<p>The list can contain another list (sub-list), which in turn contains another list and so on. This is termed a nested list.<\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code29\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-31\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">mylist = &#91;<span class=\"hljs-number\">3<\/span>, <span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">5<\/span>, <span class=\"hljs-number\">6<\/span>, <span class=\"hljs-number\">3<\/span>, &#91;<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">3<\/span>], <span class=\"hljs-number\">4<\/span>]<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-31\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code29', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code29');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p>In order to retrieve the elements of the inner list we need a nested <code>For-Loop<\/code>.<\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code30\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-32\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">nestedlist = &#91;&#91;<span class=\"hljs-number\">2<\/span>,<span class=\"hljs-number\">4<\/span>,<span class=\"hljs-number\">6<\/span>,<span class=\"hljs-number\">8<\/span>,<span class=\"hljs-number\">10<\/span>],&#91;<span class=\"hljs-number\">1<\/span>,<span class=\"hljs-number\">3<\/span>,<span class=\"hljs-number\">5<\/span>,<span class=\"hljs-number\">7<\/span>,<span class=\"hljs-number\">9<\/span>]]\n\nprint(<span class=\"hljs-string\">\"Accessing the third element of the second list\"<\/span>,nestedlist&#91;<span class=\"hljs-number\">1<\/span>]&#91;<span class=\"hljs-number\">2<\/span>])\n<span class=\"hljs-keyword\">for<\/span> i <span class=\"hljs-keyword\">in<\/span> nestedlist:\n  print(<span class=\"hljs-string\">\"list\"<\/span>,i,<span class=\"hljs-string\">\"elements\"<\/span>)\n  <span class=\"hljs-keyword\">for<\/span> j <span class=\"hljs-keyword\">in<\/span> i:\n    print(j)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-32\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code30', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code30');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Accessing the third element of the second list 5\nlist [2, 4, 6, 8, 10] elements\n2\n4\n6\n8\n10\nlist [1, 3, 5, 7, 9] elements\n1\n3\n5\n7\n9<\/pre>\n\n\n\n<p>As we can see in the above output the indexing of the nested lists with the index value of the outer loop first followed by the inner list. We can print values of the inner lists through a nested <code>for-loop<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-list-comprehension\">List Comprehension<\/h2>\n\n\n\n<p>List comprehension is a simpler method to create a list from an existing list. It is generally a list of iterables generated with an option to include only the items which satisfy a condition.<\/p>\n\n\n<pre id=\"syntax\" class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-33\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">outputList = {expression(variable) <span class=\"hljs-keyword\">for<\/span> variable <span class=\"hljs-keyword\">in<\/span> inputList &#91;<span class=\"hljs-keyword\">if<\/span> variable condition1]&#91;<span class=\"hljs-keyword\">if<\/span> variable condition2]<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-33\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<ul class=\"wp-block-list\"><li><code>expression<\/code>: Optional. expression to compute the members of the output List which satisfies the optional conditions<\/li><li><code>variable<\/code>: Required. a variable that represents the members of the input List.<\/li><li><code>inputList<\/code>: Required. Represents the input set.<\/li><li><code>condition1<\/code>, <code>condition2<\/code> etc; : Optional. Filter conditions for the members of the output List.<\/li><\/ul>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code31\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-34\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">inputList = &#91;<span class=\"hljs-number\">4<\/span>,<span class=\"hljs-number\">7<\/span>,<span class=\"hljs-number\">11<\/span>,<span class=\"hljs-number\">13<\/span>,<span class=\"hljs-number\">18<\/span>,<span class=\"hljs-number\">20<\/span>]\n<span class=\"hljs-comment\">#creating a list with square values of only the even numbers<\/span>\nsquareList = &#91;var**<span class=\"hljs-number\">2<\/span> <span class=\"hljs-keyword\">for<\/span> var <span class=\"hljs-keyword\">in<\/span> inputList <span class=\"hljs-keyword\">if<\/span> var%<span class=\"hljs-number\">2<\/span>==<span class=\"hljs-number\">0<\/span>]\nprint(squareList)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-34\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code31', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code31');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[16, 324, 400]<\/pre>\n\n\n\n<p>As seen in the above example we have created a new list from an existing input list in a single statement. The new list now contains only the squares of the even numbers present in the input list.<\/p>\n\n\n\n<p>We can even create a list when the input is a continuous&nbsp;range of numbers.<\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code32\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-35\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-comment\">#creating even square list for a range of numbers<\/span>\nsquarelist1 = &#91;s**<span class=\"hljs-number\">2<\/span> <span class=\"hljs-keyword\">for<\/span> s <span class=\"hljs-keyword\">in<\/span> range(<span class=\"hljs-number\">10<\/span>)<span class=\"hljs-keyword\">if<\/span> s%<span class=\"hljs-number\">2<\/span> == <span class=\"hljs-number\">0<\/span>]\nprint(squarelist1)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-35\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code32', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code32');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[0, 4, 16, 36, 64]<\/pre>\n\n\n\n<p>As seen in the above example we have created a list of squares of only even numbers in a range. The output is again a list so the items will be ordered.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-summary-of-list-operations\">Summary of List operations<\/h2>\n\n\n\n<p>For the following examples, we assume that <code>l1<\/code> and&nbsp;<code>l2<\/code> are lists, <code>x<\/code>,&nbsp;<code>i<\/code>,&nbsp;<code>j<\/code>,&nbsp;<code>k<\/code>,&nbsp;<code>n<\/code>&nbsp;are integers.<\/p>\n\n\n\n<p><code>l1 = [10, 20, 30, 40, 50]<\/code> and&nbsp;<code>l2 = [60, 70, 80, 60]<\/code><\/p>\n\n\n\n<figure class=\"wp-block-table is-style-regular\"><table><thead><tr><th><strong>Operation<\/strong><\/th><th>Description<\/th><\/tr><\/thead><tbody><tr><td><code>x in l1<\/code><\/td><td>Check if the list <code>l1<\/code> contains item <code>x<\/code>.<\/td><\/tr><tr><td><code>x not in l2<\/code><\/td><td>Check if list <code>l1<\/code> does not contain item <code>x<\/code>.<\/td><\/tr><tr><td><code>l1 + l2<\/code><\/td><td>Concatenate the lists <code>l1<\/code> and <code>l2<\/code>. Creates a new list containing the items from <code>l1<\/code> and <code>l2<\/code>.<\/td><\/tr><tr><td><code>l1 * 5<\/code><\/td><td>Repeat the list <code>l1<\/code> 5 times.<\/td><\/tr><tr><td><code>l1[i]<\/code><\/td><td>Get the item at index <code>i<\/code>. Example <code>l1[2]<\/code> is 30.<\/td><\/tr><tr><td><code>l1[i:j]<\/code><\/td><td>List slicing. Get the items from index <code>i<\/code> up to index <code>j<\/code> (excluding <code>j<\/code>) as a List. An example <code>l1[0:2]<\/code> is <code>[10, 20]<\/code><\/td><\/tr><tr><td><code>l1[i:j:k]<\/code><\/td><td>List slicing with step. Returns a List with the items from index <code>i<\/code> up to index j taking every k-th item. An example <code>l1[0:4:2]<\/code> is [10, 30].<\/td><\/tr><tr><td><code>len(l1)<\/code><\/td><td>Returns a count of total items in a list.<\/td><\/tr><tr><td><code>l2.count(60)<\/code><\/td><td>Returns the number of times a particular item (60) appears in a list. The answer is 2.<\/td><\/tr><tr><td><code>l1.index(30)<\/code><\/td><td>Returns the index number of a particular item (30) in a list. The answer is 2.<\/td><\/tr><tr><td><code>l1.index(30, 2, 5)<\/code><\/td><td>Returns the index number of a particular item (30) in a list. But search Returns the item with maximum value from a list. The answer is 60 only from index number 2 to 5.<\/td><\/tr><tr><td><code>min(l1)<\/code><\/td><td>Returns the item with a minimum value from a list. The answer is 10.<\/td><\/tr><tr><td><code>max(l1)<\/code><\/td><td>Returns the item with maximum value from a list. The answer is 60.<\/td><\/tr><tr><td><code>l1.append(100)<\/code><\/td><td>Add item at the end of the list<\/td><\/tr><tr><td><code>l1.append([2, 5, 7])<\/code><\/td><td>Append the nested list at the end<\/td><\/tr><tr><td><code>l1[2] = 40<\/code><\/td><td>Modify the item present at index 2<\/td><\/tr><tr><td><code>l1.remove(40)<\/code><\/td><td>Removes the first occurrence of item 40 from the list.<\/td><\/tr><tr><td><code>pop(2)<\/code><\/td><td>Removes and returns the item at index 2 from the list.<\/td><\/tr><tr><td><code>l1.clear()<\/code><\/td><td>Make list empty<\/td><\/tr><tr><td><code>l3= l1.copy()<\/code><\/td><td>Copy <code>l1<\/code> into <code>l2<\/code><\/td><\/tr><\/tbody><\/table><figcaption>Summary of Python List Operations<\/figcaption><\/figure>\n\n\n\n<p><strong><br><\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python list is an ordered sequence of items. In this article you will learn the different methods of creating a list, adding, modifying, and deleting elements in the list. Also, learn how to iterate the list and access the elements in the list in detail. Nested Lists and List Comprehension are also discussed in detail [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_genesis_hide_title":false,"_genesis_hide_breadcrumbs":false,"_genesis_hide_singular_image":false,"_genesis_hide_footer_widgets":false,"_genesis_custom_body_class":"","_genesis_custom_post_class":"","_genesis_layout":"","footnotes":""},"categories":[22,1],"tags":[32],"class_list":{"0":"post-8765","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-python","7":"category-basics","8":"tag-python-basics","9":"entry"},"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.3 (Yoast SEO v27.3) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Python Lists &#8211; PYnative<\/title>\n<meta name=\"description\" content=\"Creating a Python list. adding, modifying, and deleting elements in the list. Learn Nested Lists and List Comprehension\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/pynative.com\/python-lists\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Lists\" \/>\n<meta property=\"og:description\" content=\"Creating a Python list. adding, modifying, and deleting elements in the list. Learn Nested Lists and List Comprehension\" \/>\n<meta property=\"og:url\" content=\"https:\/\/pynative.com\/python-lists\/\" \/>\n<meta property=\"og:site_name\" content=\"PYnative\" \/>\n<meta property=\"article:published_time\" content=\"2021-03-03T07:23:09+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-04-09T05:30:47+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/pynative.com\/wp-content\/uploads\/2021\/03\/python-list.jpg\" \/>\n<meta name=\"author\" content=\"Vishal\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@PyNative\" \/>\n<meta name=\"twitter:site\" content=\"@PyNative\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Vishal\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"18 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/pynative.com\\\/python-lists\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/pynative.com\\\/python-lists\\\/\"},\"author\":{\"name\":\"Vishal\",\"@id\":\"https:\\\/\\\/pynative.com\\\/#\\\/schema\\\/person\\\/64b55d5bde2265918c5a9931de4de71f\"},\"headline\":\"Python Lists\",\"datePublished\":\"2021-03-03T07:23:09+00:00\",\"dateModified\":\"2021-04-09T05:30:47+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/pynative.com\\\/python-lists\\\/\"},\"wordCount\":2599,\"commentCount\":40,\"publisher\":{\"@id\":\"https:\\\/\\\/pynative.com\\\/#\\\/schema\\\/person\\\/64b55d5bde2265918c5a9931de4de71f\"},\"image\":{\"@id\":\"https:\\\/\\\/pynative.com\\\/python-lists\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/pynative.com\\\/wp-content\\\/uploads\\\/2021\\\/03\\\/python-list.jpg\",\"keywords\":[\"Python Basics\"],\"articleSection\":[\"Python\",\"Python Basics\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/pynative.com\\\/python-lists\\\/#respond\"]}],\"accessibilityFeature\":[\"tableOfContents\"]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/pynative.com\\\/python-lists\\\/\",\"url\":\"https:\\\/\\\/pynative.com\\\/python-lists\\\/\",\"name\":\"Python Lists &#8211; PYnative\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/pynative.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/pynative.com\\\/python-lists\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/pynative.com\\\/python-lists\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/pynative.com\\\/wp-content\\\/uploads\\\/2021\\\/03\\\/python-list.jpg\",\"datePublished\":\"2021-03-03T07:23:09+00:00\",\"dateModified\":\"2021-04-09T05:30:47+00:00\",\"description\":\"Creating a Python list. adding, modifying, and deleting elements in the list. Learn Nested Lists and List Comprehension\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/pynative.com\\\/python-lists\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/pynative.com\\\/python-lists\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/pynative.com\\\/python-lists\\\/#primaryimage\",\"url\":\"https:\\\/\\\/pynative.com\\\/wp-content\\\/uploads\\\/2021\\\/03\\\/python-list.jpg\",\"contentUrl\":\"https:\\\/\\\/pynative.com\\\/wp-content\\\/uploads\\\/2021\\\/03\\\/python-list.jpg\",\"width\":676,\"height\":395,\"caption\":\"Python List\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/pynative.com\\\/python-lists\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/pynative.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python\",\"item\":\"https:\\\/\\\/pynative.com\\\/python\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Python Lists\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/pynative.com\\\/#website\",\"url\":\"https:\\\/\\\/pynative.com\\\/\",\"name\":\"PYnative\",\"description\":\"Python Programming\",\"publisher\":{\"@id\":\"https:\\\/\\\/pynative.com\\\/#\\\/schema\\\/person\\\/64b55d5bde2265918c5a9931de4de71f\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/pynative.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/pynative.com\\\/#\\\/schema\\\/person\\\/64b55d5bde2265918c5a9931de4de71f\",\"name\":\"Vishal\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/pynative.com\\\/wp-content\\\/uploads\\\/2019\\\/01\\\/vishalHule.jpg\",\"url\":\"https:\\\/\\\/pynative.com\\\/wp-content\\\/uploads\\\/2019\\\/01\\\/vishalHule.jpg\",\"contentUrl\":\"https:\\\/\\\/pynative.com\\\/wp-content\\\/uploads\\\/2019\\\/01\\\/vishalHule.jpg\",\"width\":968,\"height\":1065,\"caption\":\"Vishal\"},\"logo\":{\"@id\":\"https:\\\/\\\/pynative.com\\\/wp-content\\\/uploads\\\/2019\\\/01\\\/vishalHule.jpg\"},\"description\":\"Founder of PYnative.com. I am a Python developer and I love to write articles to help developers. | All the best for your future Python endeavors!\",\"sameAs\":[\"https:\\\/\\\/pynative.com\"]}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Python Lists &#8211; PYnative","description":"Creating a Python list. adding, modifying, and deleting elements in the list. Learn Nested Lists and List Comprehension","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:\/\/pynative.com\/python-lists\/","og_locale":"en_US","og_type":"article","og_title":"Python Lists","og_description":"Creating a Python list. adding, modifying, and deleting elements in the list. Learn Nested Lists and List Comprehension","og_url":"https:\/\/pynative.com\/python-lists\/","og_site_name":"PYnative","article_published_time":"2021-03-03T07:23:09+00:00","article_modified_time":"2021-04-09T05:30:47+00:00","og_image":[{"url":"https:\/\/pynative.com\/wp-content\/uploads\/2021\/03\/python-list.jpg","type":"","width":"","height":""}],"author":"Vishal","twitter_card":"summary_large_image","twitter_creator":"@PyNative","twitter_site":"@PyNative","twitter_misc":{"Written by":"Vishal","Est. reading time":"18 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/pynative.com\/python-lists\/#article","isPartOf":{"@id":"https:\/\/pynative.com\/python-lists\/"},"author":{"name":"Vishal","@id":"https:\/\/pynative.com\/#\/schema\/person\/64b55d5bde2265918c5a9931de4de71f"},"headline":"Python Lists","datePublished":"2021-03-03T07:23:09+00:00","dateModified":"2021-04-09T05:30:47+00:00","mainEntityOfPage":{"@id":"https:\/\/pynative.com\/python-lists\/"},"wordCount":2599,"commentCount":40,"publisher":{"@id":"https:\/\/pynative.com\/#\/schema\/person\/64b55d5bde2265918c5a9931de4de71f"},"image":{"@id":"https:\/\/pynative.com\/python-lists\/#primaryimage"},"thumbnailUrl":"https:\/\/pynative.com\/wp-content\/uploads\/2021\/03\/python-list.jpg","keywords":["Python Basics"],"articleSection":["Python","Python Basics"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/pynative.com\/python-lists\/#respond"]}],"accessibilityFeature":["tableOfContents"]},{"@type":"WebPage","@id":"https:\/\/pynative.com\/python-lists\/","url":"https:\/\/pynative.com\/python-lists\/","name":"Python Lists &#8211; PYnative","isPartOf":{"@id":"https:\/\/pynative.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/pynative.com\/python-lists\/#primaryimage"},"image":{"@id":"https:\/\/pynative.com\/python-lists\/#primaryimage"},"thumbnailUrl":"https:\/\/pynative.com\/wp-content\/uploads\/2021\/03\/python-list.jpg","datePublished":"2021-03-03T07:23:09+00:00","dateModified":"2021-04-09T05:30:47+00:00","description":"Creating a Python list. adding, modifying, and deleting elements in the list. Learn Nested Lists and List Comprehension","breadcrumb":{"@id":"https:\/\/pynative.com\/python-lists\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/pynative.com\/python-lists\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/pynative.com\/python-lists\/#primaryimage","url":"https:\/\/pynative.com\/wp-content\/uploads\/2021\/03\/python-list.jpg","contentUrl":"https:\/\/pynative.com\/wp-content\/uploads\/2021\/03\/python-list.jpg","width":676,"height":395,"caption":"Python List"},{"@type":"BreadcrumbList","@id":"https:\/\/pynative.com\/python-lists\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/pynative.com\/"},{"@type":"ListItem","position":2,"name":"Python","item":"https:\/\/pynative.com\/python\/"},{"@type":"ListItem","position":3,"name":"Python Lists"}]},{"@type":"WebSite","@id":"https:\/\/pynative.com\/#website","url":"https:\/\/pynative.com\/","name":"PYnative","description":"Python Programming","publisher":{"@id":"https:\/\/pynative.com\/#\/schema\/person\/64b55d5bde2265918c5a9931de4de71f"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/pynative.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/pynative.com\/#\/schema\/person\/64b55d5bde2265918c5a9931de4de71f","name":"Vishal","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/pynative.com\/wp-content\/uploads\/2019\/01\/vishalHule.jpg","url":"https:\/\/pynative.com\/wp-content\/uploads\/2019\/01\/vishalHule.jpg","contentUrl":"https:\/\/pynative.com\/wp-content\/uploads\/2019\/01\/vishalHule.jpg","width":968,"height":1065,"caption":"Vishal"},"logo":{"@id":"https:\/\/pynative.com\/wp-content\/uploads\/2019\/01\/vishalHule.jpg"},"description":"Founder of PYnative.com. I am a Python developer and I love to write articles to help developers. | All the best for your future Python endeavors!","sameAs":["https:\/\/pynative.com"]}]}},"featured_image_src":null,"featured_image_src_square":null,"author_info":{"display_name":"Vishal","author_link":"https:\/\/pynative.com\/author\/vishal\/"},"_links":{"self":[{"href":"https:\/\/pynative.com\/wp-json\/wp\/v2\/posts\/8765","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pynative.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pynative.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pynative.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/pynative.com\/wp-json\/wp\/v2\/comments?post=8765"}],"version-history":[{"count":0,"href":"https:\/\/pynative.com\/wp-json\/wp\/v2\/posts\/8765\/revisions"}],"wp:attachment":[{"href":"https:\/\/pynative.com\/wp-json\/wp\/v2\/media?parent=8765"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pynative.com\/wp-json\/wp\/v2\/categories?post=8765"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pynative.com\/wp-json\/wp\/v2\/tags?post=8765"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}