{"id":74708,"date":"2020-01-03T11:00:26","date_gmt":"2020-01-03T05:30:26","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=74708"},"modified":"2020-01-03T11:00:26","modified_gmt":"2020-01-03T05:30:26","slug":"python-lists","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/python-lists\/","title":{"rendered":"Python Lists &#8211;  Learn to store multiple values in Python"},"content":{"rendered":"<p>In this article, let\u2019s learn about Python Lists.<\/p>\n<p>List is one of the most <strong>powerful<\/strong> data structures in Python. The List data type is made with so much of efforts and every programmer from <strong>beginner<\/strong>, <strong>intermediate<\/strong> to an <strong>expert<\/strong> should understand how it works.<\/p>\n<p>So let\u2019s go ahead and dive into the lists.<\/p>\n<h3>What are Python Lists?<\/h3>\n<p>Python does not have <strong>arrays<\/strong> like <strong>Java<\/strong> or <strong>C++<\/strong>. Arrays are data structures which hold <strong>multiple values<\/strong>.<\/p>\n<p>Python does not have arrays but it has <strong>lists<\/strong>. Lists are <strong>mutable collections<\/strong> of <strong>objects<\/strong>.<\/p>\n<p>This is an example:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">values=['milk','cheese',12, False]<\/pre>\n<ul>\n<li>Lists are <strong>ordered<\/strong>. We can index them and access values. We will see how to do this in the next heading.<\/li>\n<li>Lists are <strong>heterogeneous<\/strong>. A list can contain different types of elements.<\/li>\n<li>Lists are <strong>mutable<\/strong>. You can change values in them.<\/li>\n<\/ul>\n<h3>Creating Lists in Python<\/h3>\n<p>You can create lists in many ways:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; items=[1,2,3]\n&gt;&gt;&gt; items<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">[1, 2, 3]<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; items=list('123')\n&gt;&gt;&gt; items<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">[&#8216;1&#8217;, &#8216;2&#8217;, &#8216;3&#8217;]<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; items=['123']\n&gt;&gt;&gt; items<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">[&#8216;123&#8217;]<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; items=[]\n&gt;&gt;&gt; items.append(7)\n&gt;&gt;&gt; items<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">[7]<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; items=[1, 2, False, [4, 5]]\n&gt;&gt;&gt; items<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">[1, 2, False, [4, 5]]<\/div>\n<p>You can use the <strong>in-built list() function<\/strong> to convert another <strong>data type<\/strong> into a <strong>list<\/strong>. It can also create an <strong>empty list<\/strong> without <strong>parameters<\/strong>.<\/p>\n<h3>Indexing Lists in Python<\/h3>\n<p>Like strings, we can <strong>index lists<\/strong> to access their <strong>values<\/strong>. Indexing starts at <strong>0<\/strong>.<\/p>\n<p>Let\u2019s take an example.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; values<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">[&#8216;milk&#8217;, &#8216;cheese&#8217;, 12, False]<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; values[2]<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">12<\/div>\n<p>12 is the value at index 2.<\/p>\n<h4>1. Positive Indexing<\/h4>\n<p>Indexing <strong>left to right<\/strong>, starts at <strong>0<\/strong>.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; values[1]<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">&#8216;cheese&#8217;<\/div>\n<h4>2. Negative Indexing<\/h4>\n<p>Indexing <strong>right to left<\/strong>, starts at <strong>-1<\/strong>.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; values[-1]<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">False<\/div>\n<h3>Slicing Lists in Python<\/h3>\n<p>We can slice lists as well. This gives us only a <strong>part<\/strong> of the list. Slicing can take one, two or three values &#8211; <strong>start<\/strong>, <strong>stop<\/strong> and <strong>step<\/strong>.<\/p>\n<p>Let\u2019s take examples here as well.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; values #list<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">[&#8216;milk&#8217;, &#8216;cheese&#8217;, 12, False]<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; values[:] #complete list<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">[&#8216;milk&#8217;, &#8216;cheese&#8217;, 12, False]<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; values[:2] #upto index 1<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">[&#8216;milk&#8217;, &#8216;cheese&#8217;]<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; values[2:] #from index 2 to end<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">[12, False]<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; values[-3:] #from index -3 to end<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">[&#8216;cheese&#8217;, 12, False]<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; values[1::2] #from index 1 to end with a step of 2<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">[&#8216;cheese&#8217;, False]<\/div>\n<h3>Modifying Lists in Python<\/h3>\n<p>Unlike strings, lists are <strong>mutable<\/strong>. You can change its <strong>values<\/strong>.<\/p>\n<p>Let us take a few examples.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; values<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">[&#8216;milk&#8217;, &#8216;cheese&#8217;, 12, False]<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; values[1]='bread'\n&gt;&gt;&gt; values<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">[&#8216;milk&#8217;, &#8216;bread&#8217;, 12, False]<\/div>\n<p>values[1] is now \u2018bread\u2019 and not \u2018cheese\u2019.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; values[1:1]=['cheese','eggs']\n&gt;&gt;&gt; values<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">[&#8216;milk&#8217;, &#8216;cheese&#8217;, &#8216;eggs&#8217;, &#8216;bread&#8217;, 12, False]<\/div>\n<p>If we assign this list to the <strong>slice values[1:1]<\/strong>, it adds the values \u2018cheese\u2019 and \u2018eggs\u2019 between indices <strong>0<\/strong> and <strong>1<\/strong>.<\/p>\n<h3>Adding Elements in Python Lists<\/h3>\n<p>Lists are <strong>mutable<\/strong>, so we can also add elements later. We can do this in many ways.<\/p>\n<h4>1. append()<\/h4>\n<p>We can append values to the <strong>end<\/strong> of the list. We use the <strong>append()<\/strong> method for this. You can use this to append single values to the list &#8211; <strong>value<\/strong>, <strong>list<\/strong>, <strong>tuple<\/strong>.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; nums=[1,2,3]\n&gt;&gt;&gt; nums.append(4)\n&gt;&gt;&gt; nums.append([5,6])\n&gt;&gt;&gt; nums.append((7,8))\n&gt;&gt;&gt; nums<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">[1, 2, 3, 4, [5, 6], (7, 8)]<\/div>\n<h4>2. insert()<\/h4>\n<p>You can insert values in a list with the <strong>insert() method<\/strong>. Here, you specify a value to insert at a <strong>specific position<\/strong>.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; nums=[1,2,3]\n&gt;&gt;&gt; nums.insert(2,4)\n&gt;&gt;&gt; nums<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">[1, 2, 4, 3]<\/div>\n<h4>3. extend()<\/h4>\n<p>extend() can <strong>add multiple<\/strong> items to a list.<\/p>\n<p>Learn by example:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; nums=[1,2,3]\n&gt;&gt;&gt; nums.extend([[4,5,6],(7,8),9])\n&gt;&gt;&gt; nums<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">[1, 2, 3, [4, 5, 6], (7, 8), 9]<\/div>\n<h3>Deleting Elements in Python Lists<\/h3>\n<p>Lists have methods for deleting elements. They are <strong>remove()<\/strong> and <strong>pop()<\/strong> and are slightly different from each other.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; nums=[1,2,3,2,4,5,3,6,7,8]\n&gt;&gt;&gt; nums.remove(2)\n&gt;&gt;&gt; nums<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">[1, 3, 2, 4, 5, 3, 6, 7, 8]<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; nums.pop()<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">8<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; nums.pop()<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">7<\/div>\n<p>Here in this example, we say we want to remove the value 2 from this list.<\/p>\n<p>It removes the <strong>first occurrence<\/strong> of 2 at index 1 but does not remove the <strong>second one<\/strong> at index 3. By default, <strong>pop()<\/strong> removes the <strong>last element<\/strong> in the list. You cannot remove an element that is not in the <strong>list<\/strong>.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; nums.remove(0)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">Traceback (most recent call last):<br \/>\n<b>\u00a0\u00a0<\/b>File &#8220;&lt;pyshell#148&gt;&#8221;, line 1, in &lt;module&gt;<br \/>\n<b>\u00a0\u00a0\u00a0\u00a0<\/b>nums.remove(0)<br \/>\nValueError: list.remove(x): x not in list<\/div>\n<p>We can also give <strong>pop()<\/strong> and index to make it <strong>remove<\/strong> an element from a <strong>specific index<\/strong>.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; nums<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">[1, 3, 2, 4, 5, 3, 6]<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; nums.pop(5)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">3<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; nums<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">[1, 3, 2, 4, 5, 6]<\/div>\n<h3>Looping on Lists in Python<\/h3>\n<p>Since lists are <strong>collections<\/strong> of <strong>elements<\/strong>, we can use them in for-loops. You can do this if you want to do something for <strong>every value<\/strong> in a list.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">for num in [1,2,3,4,5,6]: \n    print(num)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">1<br \/>\n2<br \/>\n3<br \/>\n4<br \/>\n5<br \/>\n6<\/div>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">nums=[1,2,3,4,5,6]\nfor index in range(len(nums)):\n    print(nums[index])<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">1<br \/>\n2<br \/>\n3<br \/>\n4<br \/>\n5<br \/>\n6<\/div>\n<h3>Python Lists Length<\/h3>\n<p>Like for strings, we can calculate the <strong>length<\/strong> of a <strong>list<\/strong>. For this, we use the<strong> in-built len() function<\/strong>.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; nums=[1,2,3,4,5]\n&gt;&gt;&gt; len(nums)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">5<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">for index in range(len(nums)):\n    print(index,nums[index])<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">0 1<br \/>\n1 2<br \/>\n2 3<br \/>\n3 4<br \/>\n4 5<\/div>\n<h3>Concatenating, Repetition and Membership<\/h3>\n<p>We can join two lists with the <strong>+ operator<\/strong>. It <strong>adds<\/strong> <strong>integers<\/strong> and <strong>floats<\/strong> but <strong>concatenates strings<\/strong> and <strong>lists<\/strong>.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; [1,2,3]+[4,5]+[6]<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">[1, 2, 3, 4, 5, 6]<\/div>\n<p>We can make a list <strong>repeat<\/strong> by <strong>multiplying<\/strong> it by an <strong>integer<\/strong>. We saw this in strings.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; [1,2,3]*3<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">[1, 2, 3, 1, 2, 3, 1, 2, 3]<\/div>\n<p>And we can also check whether a <strong>value<\/strong> is in a list. For this, we use the <strong>in operator<\/strong>.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; [1,2] in [1,2,3]<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">False<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; 2 in [1,2,3]<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">True<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; '' not in []<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">True<\/div>\n<h3>Copying Lists in Python<\/h3>\n<p>The list class has an<strong> in-built copy() method<\/strong> which creates a <strong>copy<\/strong> of a list.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; nums=[1,2,3,4]\n&gt;&gt;&gt; nums1=nums.copy()\n&gt;&gt;&gt; nums1<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">[1, 2, 3, 4]<\/div>\n<p>nums1 is a shallow copy of <strong>nums<\/strong>. If we change a <strong>value<\/strong> in nums1, it will not affect nums.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; nums1[2]=7\n&gt;&gt;&gt; nums<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">[1, 2, 3, 4]<\/div>\n<h3>Nested Lists and Comprehension<\/h3>\n<p>Lists are <strong>heterogeneous collections<\/strong> and we can make <strong>lists hold lists<\/strong>.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; nums=[1,2,3,[4,[5,6,7],8,[9,10]]]<\/pre>\n<p>Now let\u2019s talk about list comprehension.<\/p>\n<p>This is a <strong>shortcut<\/strong> to create a list.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; nums=[num for num in range(1,8)]\n&gt;&gt;&gt; nums<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">[1, 2, 3, 4, 5, 6, 7]<\/div>\n<p>Here, for each number from 1 to 7, we <strong>add<\/strong> it to the list nums.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; nums=[num**2 for num in range(1,8)]\n&gt;&gt;&gt; nums<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">[1, 4, 9, 16, 25, 36, 49]<\/div>\n<p>And this is a list of <strong>squares<\/strong> of numbers from 1 to 7.<\/p>\n<p>You can add<strong> if-conditions<\/strong> to a list comprehension:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; nums=[num for num in range(1,8) if num%2==0]\n&gt;&gt;&gt; nums<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">[2, 4, 6]<\/div>\n<h4>1. Nested List Comprehension<\/h4>\n<p>You cannot convert all <strong>nested loops<\/strong> to <strong>list comprehension<\/strong>, but when you can:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; nums=[[num for num in range(1,8)] for val in [2,3,4]]\n&gt;&gt;&gt; nums<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">[[1, 2, 3, 4, 5, 6, 7], [1, 2, 3, 4, 5, 6, 7], [1, 2, 3, 4, 5, 6, 7]]<\/div>\n<h3>Methods on Lists in Python<a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2019\/12\/python-list-methods.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-74931 size-full\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2019\/12\/python-list-methods.jpg\" alt=\"python list methods\" width=\"671\" height=\"829\" \/><\/a><\/h3>\n<p>Like we said before, we have <strong>in-built<\/strong> methods for lists.<\/p>\n<p>Let\u2019s call them.<\/p>\n<h4>1. append()<\/h4>\n<p>This appends a <strong>single<\/strong> value to the <strong>end<\/strong> of a list.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; nums=[1,2,3,4,5]\n&gt;&gt;&gt; nums.append(6)\n&gt;&gt;&gt; nums<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">[1, 2, 3, 4, 5, 6]<\/div>\n<h4>2. extend()<\/h4>\n<p>This adds the elements of a <strong>list<\/strong> to <strong>another list<\/strong>.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; nums=[1,2,3,4,5]\n&gt;&gt;&gt; nums.extend([3,4,[5,6],7])\n&gt;&gt;&gt; nums<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">[1, 2, 3, 4, 5, 3, 4, [5, 6], 7]<\/div>\n<h4>3. insert()<\/h4>\n<p>This inserts an element at a <strong>specified index<\/strong>.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; nums=[1,2,3,4,5]\n&gt;&gt;&gt; nums.insert(6,6)\n&gt;&gt;&gt; nums<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">[1, 2, 3, 4, 5, 6]<\/div>\n<h4>4. remove()<\/h4>\n<p>This removes the <strong>specified item<\/strong> from the list.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; nums=[1,2,3,4,5]\n&gt;&gt;&gt; nums.remove(4)\n&gt;&gt;&gt; nums<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">[1, 2, 3, 5]<\/div>\n<h4>5. pop()<\/h4>\n<p>This <strong>removes<\/strong> and <strong>returns<\/strong> an element at the specified index.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; nums=[1,2,3,4,5]\n&gt;&gt;&gt; nums.pop(3)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">4<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; nums.pop()<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">5<\/div>\n<h4>6. clear()<\/h4>\n<p>This <strong>removes all items<\/strong> from the list.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; nums=[1,2,3,4,5]\n&gt;&gt;&gt; nums.clear()\n&gt;&gt;&gt; nums<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">[]<\/div>\n<h4>7. index()<\/h4>\n<p>This returns the index of the <strong>first match<\/strong> of an item.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; nums=[1,2,3,2,5]\n&gt;&gt;&gt; nums.index(2)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">1<\/div>\n<h4>8. count()<\/h4>\n<p>This returns the <strong>number of occurrences<\/strong> of the specified item in a list.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; nums=[1,2,3,2,5]\n&gt;&gt;&gt; nums.count(2)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">2<\/div>\n<h4>9. sort()<\/h4>\n<p>This sorts the list in <strong>ascending order<\/strong>.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; nums=[2,1,5,3,2]\n&gt;&gt;&gt; nums.sort()\n&gt;&gt;&gt; nums<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">[1, 2, 2, 3, 5]<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; nums.sort(reverse=True)\n&gt;&gt;&gt; nums<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">[5, 3, 2, 2, 1]<\/div>\n<h4>10. reverse()<\/h4>\n<p>This <strong>reverses<\/strong> a list.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; nums=[1,2,3,4,5]\n&gt;&gt;&gt; nums.reverse()\n&gt;&gt;&gt; nums<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">[5, 4, 3, 2, 1]<\/div>\n<p><strong>sort()<\/strong> and<strong> reverse()<\/strong> change the original list.<\/p>\n<h4>11. copy()<\/h4>\n<p>This returns a <strong>shallow copy<\/strong> of a list.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; nums=[1,2,3]\n&gt;&gt;&gt; nums1=nums.copy()\n&gt;&gt;&gt; nums1<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">[1, 2, 3]<\/div>\n<h3>Python List Functions<a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2019\/12\/python-list-functions.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-74932 size-full\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2019\/12\/python-list-functions.jpg\" alt=\"functions in python list\" width=\"600\" height=\"413\" \/><\/a><\/h3>\n<p>Methods can change the <strong>list<\/strong>, but functions will <strong>operate<\/strong> on a list and return a value. Python has some<strong> in-built functions<\/strong> for lists.<\/p>\n<h4>1. all()<\/h4>\n<p>This returns <strong>True<\/strong> if all elements in the list are True or if the list is <strong>empty<\/strong>.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; all([1,2,False])<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">False<\/div>\n<h4>2. any()<\/h4>\n<p>This returns <strong>True<\/strong> if at least <strong>one<\/strong> element in the list is True or if the list is <strong>not empty<\/strong>.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; all([1,2,False])<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">False<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; any([1,2,False])<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">True<\/div>\n<h4>3. enumerate()<\/h4>\n<p>This returns an <strong>enumerate<\/strong> object for the list. It gives the list items with their <strong>indices<\/strong>.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; for pair in enumerate([1,2,3]): print(pair)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">(0, 1)<br \/>\n(1, 2)<br \/>\n(2, 3)<\/div>\n<h4>4. filter()<\/h4>\n<p><strong>filter()<\/strong> takes a function and filters a <strong>sequence<\/strong> by checking each item.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; list(filter(lambda val:val%2==0, [1,2,3,4,5,6,7,8,9]))<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">[2, 4, 6, 8]<\/div>\n<h4>5. len<\/h4>\n<p>This returns the <strong>length<\/strong> of the list.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; len([1,2,[3,[5,4],6]])<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">3<\/div>\n<p>This list\u2019s length is 3, not 6, it has <strong>sublists<\/strong>.<\/p>\n<h3>Summary<\/h3>\n<p>And with this, we are done with lists in Python.<\/p>\n<p>In this article, we talked about Python lists, how to <strong>create<\/strong> them, <strong>index<\/strong> them, <strong>slice<\/strong> them, <strong>modify<\/strong> them, <strong>add<\/strong> <strong>elements<\/strong>, <strong>delete elements<\/strong>.<\/p>\n<p>Also, we have learned loop on lists, get their <strong>lengths<\/strong>, <strong>concatenate<\/strong>, <strong>repeat<\/strong>, <strong>check membership<\/strong>, <strong>copy them<\/strong>, <strong>nested lists<\/strong> and <strong>comprehension<\/strong>, <strong>methods on lists<\/strong> and some <strong>in-built functions<\/strong>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article, let\u2019s learn about Python Lists. List is one of the most powerful data structures in Python. The List data type is made with so much of efforts and every programmer from&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":74933,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1053],"tags":[1168,1169,1170,1171,1172,1173,1174,1175,1176],"class_list":["post-74708","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-creating-lists-in-python","tag-indexing-lists-in-python","tag-lists-in-python","tag-looping-on-lists","tag-python-list-functions","tag-python-lists","tag-python-lists-length","tag-slicing-lists-in-python","tag-what-are-python-lists"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Python Lists - Learn to store multiple values in Python - TechVidvan<\/title>\n<meta name=\"description\" content=\"Python Lists - Explore lists in Python, how to create, index, slice, and modify them. Also, see Python list methods and some in-built functions.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/techvidvan.com\/tutorials\/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 - Learn to store multiple values in Python - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"Python Lists - Explore lists in Python, how to create, index, slice, and modify them. Also, see Python list methods and some in-built functions.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/python-lists\/\" \/>\n<meta property=\"og:site_name\" content=\"TechVidvan\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/TechVidvan\/\" \/>\n<meta property=\"article:published_time\" content=\"2020-01-03T05:30:26+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2019\/12\/python-lists.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"802\" \/>\n\t<meta property=\"og:image:height\" content=\"420\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:site\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"8 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python Lists - Learn to store multiple values in Python - TechVidvan","description":"Python Lists - Explore lists in Python, how to create, index, slice, and modify them. Also, see Python list methods and some in-built functions.","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:\/\/techvidvan.com\/tutorials\/python-lists\/","og_locale":"en_US","og_type":"article","og_title":"Python Lists - Learn to store multiple values in Python - TechVidvan","og_description":"Python Lists - Explore lists in Python, how to create, index, slice, and modify them. Also, see Python list methods and some in-built functions.","og_url":"https:\/\/techvidvan.com\/tutorials\/python-lists\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2020-01-03T05:30:26+00:00","og_image":[{"width":802,"height":420,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2019\/12\/python-lists.jpg","type":"image\/jpeg"}],"author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@vidvantech","twitter_site":"@vidvantech","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"8 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/python-lists\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-lists\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22"},"headline":"Python Lists &#8211; Learn to store multiple values in Python","datePublished":"2020-01-03T05:30:26+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-lists\/"},"wordCount":1152,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-lists\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2019\/12\/python-lists.jpg","keywords":["Creating Lists in python","Indexing Lists in python","lists in python","Looping on Lists","python list functions","python lists","Python Lists Length","Slicing Lists in python","What are Python Lists"],"articleSection":["Python Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/python-lists\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/python-lists\/","url":"https:\/\/techvidvan.com\/tutorials\/python-lists\/","name":"Python Lists - Learn to store multiple values in Python - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-lists\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-lists\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2019\/12\/python-lists.jpg","datePublished":"2020-01-03T05:30:26+00:00","description":"Python Lists - Explore lists in Python, how to create, index, slice, and modify them. Also, see Python list methods and some in-built functions.","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-lists\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/python-lists\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/python-lists\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2019\/12\/python-lists.jpg","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2019\/12\/python-lists.jpg","width":802,"height":420,"caption":"Lists in python"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/python-lists\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Python Lists &#8211; Learn to store multiple values in Python"}]},{"@type":"WebSite","@id":"https:\/\/techvidvan.com\/tutorials\/#website","url":"https:\/\/techvidvan.com\/tutorials\/","name":"TechVidvan Blogs","description":"","publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/techvidvan.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/techvidvan.com\/tutorials\/#organization","name":"TechVidvan","url":"https:\/\/techvidvan.com\/tutorials\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","width":200,"height":50,"caption":"TechVidvan"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/TechVidvan\/","https:\/\/x.com\/vidvantech"]},{"@type":"Person","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22","name":"TechVidvan Team","description":"The TechVidvan Team delivers practical, beginner-friendly tutorials on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our experts are here to help you upskill and excel in today\u2019s tech industry."}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/74708","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/comments?post=74708"}],"version-history":[{"count":0,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/74708\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/74933"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=74708"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=74708"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=74708"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}