{"id":650,"date":"2021-02-26T13:19:31","date_gmt":"2021-02-26T07:49:31","guid":{"rendered":"https:\/\/cbsepython.in\/?p=650"},"modified":"2022-02-09T09:55:23","modified_gmt":"2022-02-09T04:25:23","slug":"list-manipulation-in-python-class-11-notes","status":"publish","type":"post","link":"https:\/\/cbsepython.in\/list-manipulation-in-python-class-11-notes\/","title":{"rendered":"List Manipulation in Python Class 11 Notes"},"content":{"rendered":"<h2>List Manipulation in Python Class 11 Notes<\/h2>\n<p><strong>What is List in Python?<\/strong><\/p>\n<h2>Python Lists<\/h2>\n<p>Like strings, lists are a sequence of values. A list is a data type that can be used to store any type and number of variables and information. The values in the list are called elements or items or list members.<\/p>\n<p>&nbsp;<\/p>\n<p>A list in Python is formed by enclosing the values inside square brackets ([]). Unlike strings, lists are mutable, i.e., values in a list can be changed or modified and can be accessed using index value enclosed in square brackets.<\/p>\n<p>&nbsp;<\/p>\n<p>Lists are great, but not suitable for every task. There are other data types out there like arrays, tuples and dictionaries which may be more suitable for certain tasks. First scope out your problem and then decided which data type fits the best.<\/p>\n<p>&nbsp;<\/p>\n<h3>Python Lists Syntax<\/h3>\n<p>&nbsp;<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">mylist = [\"C\",\"C++\",\"Java\",\"Python\",\"PHP\"]<\/pre>\n<p>&nbsp;<\/p>\n<p>The items within the list must be enclosed within square brackets, and each item must be separated by a comma. Unlike arrays, Lists can store any type of data type as well as store variables of different datatypes at the same time.<\/p>\n<p>The double quotes are not part of the syntax of the list. They are only necessary if the item is a string.<\/p>\n<p>&nbsp;<\/p>\n<h3>List Indexing<\/h3>\n<p>You can access the contents of a list by using it\u2019s index. A list is an ordered collection, so the values can be called in the order in which they were placed inside.<\/p>\n<p>&nbsp;<\/p>\n<p>Don\u2019t forget that indexing starts from zero in python. The code below will output \u201cC++\u201d.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">mylist = [\"C\",\"C++\",\"Java\",\"Python\",\"PHP\"]\r\nprint(mylist[1])\r\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre><code>C++<\/code><\/pre>\n<p>&nbsp;<\/p>\n<h3>Using negative integers will reverse the order of the list. Negative indexing starts from &#8211;<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">mylist = [\"C\",\"C++\",\"Java\",\"Python\",\"PHP\"]\r\nprint(mylist[-2])\r\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre>Python<\/pre>\n<p>&nbsp;<\/p>\n<h3>List Manipulation in Python<\/h3>\n<h3>Selecting a range of items<\/h3>\n<p>If you wish to pick up more than just one item in a python list, you can create \u201cslices\u201d. You can specify a range, by giving the starting and ending number. Everything in between will be placed into the slice.<\/p>\n<p>Note that the ending number will not be included in this slice.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">mylist = [\"C\",\"C++\",\"Java\",\"Python\",\"PHP\"]\r\nprint(mylist[2:5])<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre>['Java', 'Python', 'PHP']<\/pre>\n<p>&nbsp;<\/p>\n<p>You don\u2019t nessacerily how to define the starting point, as the default will be 0, or the start of the list. The semi-colon and ending point must be present though.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">mylist = [\"C\",\"C++\",\"Java\",\"Python\",\"PHP\"]\r\nprint(mylist[:3])<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre>['C', 'C++', 'Java']<\/pre>\n<p>&nbsp;<\/p>\n<p>It also works the other way around, where you define the start point, and leave out the end point, while keeping the semi-colon.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">mylist = [\"C\",\"C++\",\"Java\",\"Python\",\"PHP\"]\r\nprint(mylist[2:])<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre>['Java', 'Python', 'PHP']<\/pre>\n<p>&nbsp;<\/p>\n<h3>Updating an Item in a List<\/h3>\n<p>Updating an item value in a list is fairly straight forward. Refer to the index where you want to change the value and then assign a value to it.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">mylist = [\"C\",\"C++\",\"Java\",\"Python\",\"PHP\"]\r\nmylist[2]=\"Rust\"\r\nprint(mylist)\r\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre>['C', 'C++', 'Rust', 'Python', 'PHP']<\/pre>\n<p>&nbsp;<\/p>\n<h3>Adding an Item to a List<\/h3>\n<p>&nbsp;<\/p>\n<p>The append() adds an item to the end of the list.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">mylist = [\"C\",\"C++\",\"Java\",\"Python\",\"PHP\"]\r\nmylist.append(\"Rust\")\r\nprint(mylist)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre>['C', 'C++', 'Java', 'Python', 'PHP', 'Rust']<\/pre>\n<p>&nbsp;<\/p>\n<p>The insert() function takes two parameters. The first is the position you want to add a value, and the second parameter is the value itself.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">mylist = [\"C\",\"C++\",\"Java\",\"Python\",\"PHP\"]\r\nmylist.insert(2,\"Rust\")\r\nprint(mylist)\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Output:<\/strong><\/p>\n<pre>['C', 'C++', 'Rust', 'Java', 'Python', 'PHP']<\/pre>\n<p>Note that the former second position item, \u201cjava\u201d was moved forward to the third position.<\/p>\n<p>&nbsp;<\/p>\n<h3>Removing an Item from a List<\/h3>\n<p>The remove() function removes a specific item from a list.<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">mylist = [\"C\",\"C++\",\"Java\",\"Python\",\"PHP\"]\r\nmylist.remove(\"C++\")\r\nprint(mylist)\r\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre>['C', 'Java', 'Python', 'PHP']<\/pre>\n<p>Unlike remove(), the pop() function takes an index as a parameter, and removes the item at that index. However, the pop function is also often used without any parameters, in which case, it will remove the last item.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">mylist = [\"C\",\"C++\",\"Java\",\"Python\",\"PHP\"]\r\nmylist.pop(0)\r\nprint(mylist)\r\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre>['C++', 'Java', 'Python', 'PHP']<\/pre>\n<p>&nbsp;<\/p>\n<h3>Pop() function without any parameters.<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">mylist = [\"C\",\"C++\",\"Java\",\"Python\",\"PHP\"]\r\nmylist.pop()\r\nprint(mylist)\r\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre>['C', 'C++', 'Java', 'Python']<\/pre>\n<p>&nbsp;<\/p>\n<p>The del() function has two functions. Like, the pop() function it can delete an item at a specific index, as well as delete a whole list in one go.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">mylist = [\"C\",\"C++\",\"Java\",\"Python\",\"PHP\"]\r\ndel mylist\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>You can delete the value at a specific index using the del keyword in the following manner.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">mylist = [\"C\",\"C++\",\"Java\",\"Python\",\"PHP\"]\r\ndel mylist[2]\r\nprint(mylist)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre>['C', 'C++', 'Python', 'PHP']<\/pre>\n<p>&nbsp;<\/p>\n<h3>Other Useful Operations<\/h3>\n<p>&nbsp;<\/p>\n<p>To check for a specific item in a list<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">mylist = [\"C\",\"C++\",\"Java\",\"Python\",\"PHP\"]\r\nif \"C\" in mylist:\r\n    print(\"Yes\")\r\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre>Yes<\/pre>\n<p>&nbsp;<\/p>\n<h3>Finding the length of a list<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">mylist = [\"C\",\"C++\",\"Java\",\"Python\",\"PHP\"]\r\nlength = len(mylist)\r\nprint(length)\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Output:<\/strong><\/p>\n<pre>5<\/pre>\n<p>&nbsp;<\/p>\n<h3>Iterating through a list.<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">mylist = [\"C\",\"C++\",\"Java\",\"Python\",\"PHP\"]\r\nfor x in mylist:\r\n    print(x)\r\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre>C\r\nC++\r\nJava\r\nPython\r\nPHP<\/pre>\n<h3>Sorting a list<\/h3>\n<p>Using the sort function you can sort a list in ascending order. Works on both alphabets and numbers. Be careful as this function alters the list itself.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">mylist = [1,3,2,6,7,9,8,15,11]\r\nmylist.sort()\r\nprint(mylist)\r\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre>[1, 2, 3, 6, 7, 8, 9, 11, 15]<\/pre>\n<p>&nbsp;<\/p>\n<h3>Concatenating two lists:<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">mylist1 = [1,3,2,6]\r\nmylist2 = [\"apple\", \"banana\"]\r\nmylist=mylist1+mylist2\r\nprint(mylist)\r\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre>[1, 3, 2, 6, 'apple', 'banana']<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>List Manipulation in Python Class 11 Notes What is List in Python? Python Lists Like strings, lists are a sequence of values. A list is a data type that can be used to store any type and number of variables and information. The values in the list are called elements or items or list members. [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20],"tags":[],"class_list":["post-650","post","type-post","status-publish","format-standard","hentry","category-cbse-computer-science-with-python-class-12"],"_links":{"self":[{"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/posts\/650","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/comments?post=650"}],"version-history":[{"count":0,"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/posts\/650\/revisions"}],"wp:attachment":[{"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/media?parent=650"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/categories?post=650"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/tags?post=650"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}