{"id":25310,"date":"2023-03-30T14:21:13","date_gmt":"2023-03-30T08:51:13","guid":{"rendered":"https:\/\/tocxten.com\/?page_id=25310"},"modified":"2023-04-01T00:00:40","modified_gmt":"2023-03-31T18:30:40","slug":"lists","status":"publish","type":"page","link":"https:\/\/tocxten.com\/index.php\/lists\/","title":{"rendered":"Lists"},"content":{"rendered":"\n<p class=\"has-background has-medium-font-size\" style=\"background-color:#dce3e6\"><strong>Syllabus :<\/strong><\/p>\n\n\n\n<p class=\"has-medium-font-size\"><strong>Lists:<\/strong>&nbsp;The List Data Type, Working with Lists, Augmented Assignment Operators, Methods,&nbsp;<strong>Example Program:<\/strong>&nbsp;Magic 8 Ball with a List, List-like Types: Strings and Tuples, References,<\/p>\n\n\n\n<p class=\"has-medium-font-size\">In Python, a sequence is an ordered collection of elements, where each element is identified by an index. Each element of a sequence is assigned a number \u2013&nbsp;<em>its position or index<\/em>. The first index is zero, the second index is one, and so forth. <\/p>\n\n\n\n<p class=\"has-medium-font-size\">There are three types of sequences in Python: <strong>lists, tuples, and strings<\/strong>.<\/p>\n\n\n\n<p class=\"has-background has-medium-font-size\" style=\"background-color:#e2eaed\"><strong>List Data type<\/strong><\/p>\n\n\n\n<p class=\"has-medium-font-size\">In Python, a list is a collection of ordered and mutable elements. A list can contain elements of different data types, such as integers, floats, strings, and even other lists. Lists are created by enclosing a comma-separated sequence of values within square brackets [ ].<\/p>\n\n\n\n<p class=\"has-medium-font-size\"><strong>Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>my_list = &#91;1, 2, 3, \"four\", 5.0, &#91;6, 7, 8]]<\/code><\/pre>\n\n\n\n<p class=\"has-background\" style=\"background-color:#dee4e6;font-size:30px\"><strong>Different ways to create List<\/strong><\/p>\n\n\n\n<p class=\"has-medium-font-size\">In Python, there are different ways to create a list. Here are some examples:<\/p>\n\n\n\n<ol class=\"has-medium-font-size wp-block-list\"><li><strong>Using square brackets:<\/strong> You can create a list by enclosing a sequence of elements within square brackets. The elements can be of any data type, and can include integers, floats, strings, and even other lists.<\/li><\/ol>\n\n\n\n<p class=\"has-medium-font-size\">Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>my_list = &#91;1, 2, 3, \"four\", 5.0, &#91;6, 7, 8]]<\/code><\/pre>\n\n\n\n<ol start=\"2\" class=\"has-medium-font-size wp-block-list\"><li><strong>Using the list() function<\/strong>: You can create a list by passing a sequence of elements to the <code>list()<\/code> function. The sequence can be any iterable object, such as a tuple or a string.<\/li><\/ol>\n\n\n\n<p class=\"has-medium-font-size\">Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>my_tuple = (1, 2, 3, \"four\", 5.0)\nmy_list = list(my_tuple)<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\">3. <strong>Using append() method<\/strong> : The <code>append()<\/code> method adds a single element to the end of the list.<\/p>\n\n\n\n<p class=\"has-medium-font-size\">Example : <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>my_list = &#91;]\nmy_list.append(1)\nmy_list.append(2)\nmy_list.append(3)\nprint(my_list) # Output: &#91;1, 2, 3]<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\">4.<strong> Using extend() method <\/strong>: The <code><strong>extend()<\/strong><\/code> method adds multiple elements to the end of the list by appending each element from the iterable that is passed as an argument.<\/p>\n\n\n\n<p class=\"has-medium-font-size\">Example : <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>my_list = &#91;]\nmy_list.extend(&#91;1, 2, 3])\nprint(my_list) # Output: &#91;1, 2, 3]<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\">In this example, the <code>extend()<\/code> method is used to add multiple elements to the end of the list at once. The elements are passed as a list argument to the <code>extend()<\/code> method. <\/p>\n\n\n\n<p class=\"has-medium-font-size\">Note that the <code>extend()<\/code> method can also add elements from other iterable objects, such as tuples or other lists.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>my_list = &#91;]\nmy_list.extend((1, 2, 3))\nprint(my_list) # Output: &#91;1, 2, 3]\n\nother_list = &#91;4, 5, 6]\nmy_list.extend(other_list)\nprint(my_list) # Output: &#91;1, 2, 3, 4, 5, 6]<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p class=\"has-medium-font-size\"><strong>5. Using list comprehension<\/strong>: You can create a list using a list comprehension, which is a concise way to create a list based on an existing sequence.<\/p>\n\n\n\n<p class=\"has-medium-font-size\">Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>my_list = &#91;x for x in range(10)]<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\">This creates a list of integers from 0 to 9.<\/p>\n\n\n\n<p class=\"has-medium-font-size\">6.  <strong>Using the range() function<\/strong>: You can create a list of integers using the <code>range()<\/code> function, which generates a sequence of integers based on the given arguments.<\/p>\n\n\n\n<p>Example : <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>my_list = list(range(1, 10, 2))<\/code><\/pre>\n\n\n\n<p>This creates a list of odd numbers from 1 to 9.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p class=\"has-background has-medium-font-size\" style=\"background-color:#dfe5e8\">7. <strong>Using the split() method:<\/strong><\/p>\n\n\n\n<p class=\"has-medium-font-size\">You can create a list of strings by splitting a string based on a delimiter. The <code>split()<\/code> method splits a string into a list of substrings based on a specified separator.<\/p>\n\n\n\n<p class=\"has-medium-font-size\"><strong>Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>my_string = \"apple,banana,orange\"\nmy_list = my_string.split(\",\")<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\">This creates a list of strings containing the fruits &#8220;apple&#8221;, &#8220;banana&#8221;, and &#8220;orange&#8221;.<\/p>\n\n\n\n<p class=\"has-medium-font-size\">These are some of the ways to create a list in Python. Depending on the situation, one method may be more appropriate than the others.<\/p>\n\n\n\n<p>Example : <strong>Creation of Empty list <\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-113.png\" alt=\"\" class=\"wp-image-25504\" width=\"225\" height=\"175\"\/><\/figure>\n\n\n\n<p>Example : <strong>Creation of List with values<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-114.png\" alt=\"\" class=\"wp-image-25505\" width=\"413\" height=\"263\" srcset=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-114.png 644w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-114-300x191.png 300w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-114-600x382.png 600w\" sizes=\"auto, (max-width: 413px) 100vw, 413px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p class=\"has-background\" style=\"background-color:#d4dadc;font-size:30px\"><strong>Lists are Mutable<\/strong><\/p>\n\n\n\n<p class=\"has-medium-font-size\">In Python, a list is a collection of items that are ordered and changeable. One key feature of lists is that they are mutable, meaning that their elements can be modified after they are created.<\/p>\n\n\n\n<p class=\"has-medium-font-size\">Here&#8217;s an example of how a list can be modified in Python:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># create a list of fruits\nfruits = &#91;'apple', 'banana', 'orange']\n\n# add a new fruit to the list\nfruits.append('strawberry')\nprint(fruits)  # Output: &#91;'apple', 'banana', 'orange', 'strawberry']\n\n# remove an item from the list\nfruits.remove('banana')\nprint(fruits)  # Output: &#91;'apple', 'orange', 'strawberry']\n\n# change an item in the list\nfruits&#91;0] = 'kiwi'\nprint(fruits)  # Output: &#91;'kiwi', 'orange', 'strawberry']<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\">In this example, we create a list called <code>fruits<\/code> containing three elements: <strong><code>'apple'<\/code>, <code>'banana'<\/code>, and <code>'orange'<\/code><\/strong>. We then use the <code>append()<\/code> method to add a new element, <code>'strawberry'<\/code>, to the end of the list. Next, we use the <code>remove()<\/code> method to remove the <code>'banana'<\/code> element from the list. Finally, we use indexing to change the first element of the list from <code>'apple'<\/code> to <code>'kiwi'<\/code>.<\/p>\n\n\n\n<p class=\"has-medium-font-size\">This example demonstrates that lists are mutable because we are able to add, remove, and modify elements within the list after it has been created. This can be very useful in programming, as it allows us to dynamically update our data structures as needed.<\/p>\n\n\n\n<p>Example for Mutability : <\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-115.png\" alt=\"\" class=\"wp-image-25506\" width=\"278\" height=\"312\" srcset=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-115.png 415w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-115-267x300.png 267w\" sizes=\"auto, (max-width: 278px) 100vw, 278px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p class=\"has-background\" style=\"background-color:#e1e8eb;font-size:30px\"><strong>List Concatenation<\/strong><\/p>\n\n\n\n<p class=\"has-medium-font-size\">List concatenation is the process of combining two or more lists into a single list. In Python, there are multiple ways to concatenate lists. Here are some examples:<\/p>\n\n\n\n<ol class=\"has-medium-font-size wp-block-list\"><li><strong>Using the &#8216;+&#8217; operator:<\/strong><\/li><\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>list1 = &#91;1, 2, 3]\nlist2 = &#91;4, 5, 6]\nconcatenated_list = list1 + list2\nprint(concatenated_list)  # Output: &#91;1, 2, 3, 4, 5, 6]<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\">In the above example, we have two lists, <code>list1<\/code> and <code>list2<\/code>. To concatenate them, we use the &#8216;+&#8217; operator, which creates a new list that contains all the elements of both <code>list1<\/code> and <code>list2<\/code>.<\/p>\n\n\n\n<ol start=\"2\" class=\"has-medium-font-size wp-block-list\"><li><strong>Using the <code>extend()<\/code> method:<\/strong><\/li><\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>list1 = &#91;1, 2, 3]\nlist2 = &#91;4, 5, 6]\nlist1.extend(list2)\nprint(list1)  # Output: &#91;1, 2, 3, 4, 5, 6<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\"><strong>3. Using the <code>append()<\/code> method in a loop:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>list1 = &#91;1, 2, 3]\nlist2 = &#91;4, 5, 6]\nfor item in list2:\n    list1.append(item)\nprint(list1)  # Output: &#91;1, 2, 3, 4, 5, 6]<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\">In the above example, we have two lists, <code>list1<\/code> and <code>list2<\/code>. To concatenate them, we use a loop that iterates over each element of <code>list2<\/code> and appends it to <code>list1<\/code> using the <code>append()<\/code> method.<\/p>\n\n\n\n<p class=\"has-medium-font-size\">Example : <\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-116.png\" alt=\"\" class=\"wp-image-25507\" width=\"347\" height=\"250\" srcset=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-116.png 443w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-116-300x216.png 300w\" sizes=\"auto, (max-width: 347px) 100vw, 347px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p class=\"has-background\" style=\"background-color:#e1e7ea;font-size:30px\"><strong>List Replication <\/strong><\/p>\n\n\n\n<p class=\"has-medium-font-size\">List replication is a process of creating a new list by replicating the elements of an existing list multiple times. In Python, list replication can be achieved using the &#8216;*&#8217; operator. Here are some examples:<\/p>\n\n\n\n<ol class=\"has-medium-font-size wp-block-list\"><li><strong>Replicating a list multiple times:<\/strong><\/li><\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>original_list = &#91;1, 2, 3]\nreplicated_list = original_list * 3\nprint(replicated_list) # Output: &#91;1, 2, 3, 1, 2, 3, 1, 2, 3]<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\">In the above example, we have an original list <code>original_list<\/code> with three elements. We replicate this list three times using the &#8216;*&#8217; operator, which creates a new list <code>replicated_list<\/code> that contains the elements of <code>original_list<\/code> repeated three times.<\/p>\n\n\n\n<p class=\"has-medium-font-size\">2. <strong>Creating a list of a specific length with a single element repeated:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>single_element_list = &#91;0] * 5\nprint(single_element_list) # Output: &#91;0, 0, 0, 0, 0]<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\">In the above example, we create a new list <code>single_element_list<\/code> that contains the element 0 repeated five times using the &#8216;*&#8217; operator. This is a useful technique when you need to create a list of a specific length with a single element repeated.<\/p>\n\n\n\n<p class=\"has-medium-font-size\">3. <strong>Replicating a list using a variable:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>original_list = &#91;1, 2, 3]\nn = 4\nreplicated_list = original_list * n\nprint(replicated_list) # Output: &#91;1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\">In the above example, we use a variable <code>n<\/code> to specify the number of times we want to replicate the <code>original_list<\/code>. The <code>replicated_list<\/code> is created by multiplying <code>original_list<\/code> by <code>n<\/code> using the &#8216;*&#8217; operator.<\/p>\n\n\n\n<p class=\"has-medium-font-size\">Example : <\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-117.png\" alt=\"\" class=\"wp-image-25509\" width=\"471\" height=\"189\" srcset=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-117.png 606w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-117-300x120.png 300w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-117-600x241.png 600w\" sizes=\"auto, (max-width: 471px) 100vw, 471px\" \/><\/figure>\n\n\n\n<p class=\"has-background\" style=\"background-color:#dae3e6;font-size:30px\"><strong>Removing values from lists with del statement<\/strong><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p class=\"has-medium-font-size\">In Python, you can remove elements from a list using the <code>del<\/code> statement. The <code>del<\/code> statement is a powerful Python keyword that allows you to delete an object, such as a variable or an element in a list. Here are some examples of removing values from lists using the <code>del<\/code> statement:<\/p>\n\n\n\n<ol class=\"has-medium-font-size wp-block-list\"><li><strong>Removing an element by index:<\/strong><\/li><\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>my_list = &#91;1, 2, 3, 4, 5]\ndel my_list&#91;1:4]\nprint(my_list) # Output: &#91;1, 5]<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\">In the above example, we have a list <code>my_list<\/code> with five elements. We use the <code>del<\/code> statement to remove the elements at indices 1, 2, and 3, which have the values 2, 3, and 4. We do this by using slicing notation <code>[1:4]<\/code>, which selects the elements at indices 1, 2, and 3. After deleting the elements, the list contains the remaining elements <code>[1, 5]<\/code>.<\/p>\n\n\n\n<p class=\"has-medium-font-size\"><strong>2. Removing multiple elements by slicing:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>my_list = &#91;1, 2, 3, 4, 5]\ndel my_list&#91;1:4]\nprint(my_list) # Output: &#91;1, 5]<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\">In the above example, we have a list <code>my_list<\/code> with five elements. We use the <code>del<\/code> statement to remove the elements at indices 1, 2, and 3, which have the values 2, 3, and 4. We do this by using slicing notation <code>[1:4]<\/code>, which selects the elements at indices 1, 2, and 3. After deleting the elements, the list contains the remaining elements <code>[1, 5]<\/code>.<\/p>\n\n\n\n<p class=\"has-medium-font-size\">3. <strong>Removing all elements:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>my_list = &#91;1, 2, 3, 4, 5]\ndel my_list&#91;:]\nprint(my_list) # Output: &#91;]<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\">In the above example, we have a list <code>my_list<\/code> with five elements. We use the <code>del<\/code> statement to remove all elements of the list by slicing the entire list with <code>[:]<\/code>. After deleting all elements, the list is empty, <code>[]<\/code>.<\/p>\n\n\n\n<p class=\"has-medium-font-size\">Note that the <code>del<\/code> statement permanently deletes the elements from the list, so you should be careful when using it. Make sure you have a copy of the original list or are sure that you no longer need the deleted elements.<\/p>\n\n\n\n<p class=\"has-medium-font-size\"><strong>Example : <\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-89.png\" alt=\"\" class=\"wp-image-25349\" width=\"609\" height=\"457\" srcset=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-89.png 768w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-89-300x225.png 300w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-89-760x570.png 760w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-89-600x450.png 600w\" sizes=\"auto, (max-width: 609px) 100vw, 609px\" \/><\/figure>\n\n\n\n<p class=\"has-medium-font-size\"><strong>Example :<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-122.png\" alt=\"\" class=\"wp-image-25520\" width=\"699\" height=\"372\" srcset=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-122.png 868w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-122-300x160.png 300w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-122-768x409.png 768w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-122-760x405.png 760w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-122-600x319.png 600w\" sizes=\"auto, (max-width: 699px) 100vw, 699px\" \/><\/figure>\n\n\n\n<p class=\"has-medium-font-size\"><strong>Example : <\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-123.png\" alt=\"\" class=\"wp-image-25522\" width=\"572\" height=\"198\" srcset=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-123.png 768w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-123-300x104.png 300w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-123-760x263.png 760w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-123-600x208.png 600w\" sizes=\"auto, (max-width: 572px) 100vw, 572px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p class=\"has-background\" style=\"background-color:#dfe5e8;font-size:25px\"><strong>Accessing List values (Getting individual values in lists with indexes.)<\/strong><\/p>\n\n\n\n<p class=\"has-medium-font-size\">One can think List as a relationship between indices and elements. This relationship is called a mapping. Each index \u2018maps to\u2019, one of the elements. Python Supports both positive and negative indexing of list elements as illustrated below.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"283\" src=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-90-1024x283.png\" alt=\"\" class=\"wp-image-25357\" srcset=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-90-1024x283.png 1024w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-90-300x83.png 300w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-90-768x212.png 768w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-90-1536x424.png 1536w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-90-1130x312.png 1130w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-90-760x210.png 760w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-90-600x166.png 600w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-90.png 1644w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption><strong>Fig: Positive Indexing [ source<\/strong> : <a href=\"https:\/\/railsware.com\/blog\/python-for-machine-learning-indexing-and-slicing-for-lists-tuples-strings-and-other-sequential-types\/\">Link <\/a> ]<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"292\" src=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-91-1024x292.png\" alt=\"\" class=\"wp-image-25359\" srcset=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-91-1024x292.png 1024w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-91-300x85.png 300w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-91-768x219.png 768w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-91-1536x437.png 1536w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-91-1130x322.png 1130w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-91-760x216.png 760w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-91-600x171.png 600w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-91.png 1644w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption><strong>Fig: Negative Indexing [ Source<\/strong> :<a href=\"https:\/\/railsware.com\/blog\/python-for-machine-learning-indexing-and-slicing-for-lists-tuples-strings-and-other-sequential-types\/\">Link<\/a> ]<\/figcaption><\/figure>\n\n\n\n<p class=\"has-medium-font-size\">Positive indexing starts from 0 and goes up to n-1, where n is the length of the list. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>my_list = &#91;10, 20, 30, 40]\nprint(my_list&#91;0])    # Output: 10\nprint(my_list&#91;1])    # Output: 20\nprint(my_list&#91;2])    # Output: 30\nprint(my_list&#91;3])    # Output: 40<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\">Negative indexing starts from -1 and goes down to -n, where n is the length of the list. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>my_list = &#91;10, 20, 30, 40]\nprint(my_list&#91;-1])   # Output: 40\nprint(my_list&#91;-2])   # Output: 30\nprint(my_list&#91;-3])   # Output: 20\nprint(my_list&#91;-4])   # Output: 10<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\">Here are some examples that demonstrate the use of positive and negative indexing in Python lists of integers:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>my_list = &#91;10, 20, 30, 40, 50]\n\n# Accessing elements using positive indexing\nprint(my_list&#91;0])       # Output: 10\nprint(my_list&#91;2])       # Output: 30\n\n# Accessing elements using negative indexing\nprint(my_list&#91;-1])      # Output: 50\nprint(my_list&#91;-3])      # Output: 30\n\n# Modifying elements using positive indexing\nmy_list&#91;1] = 25\nprint(my_list)          # Output: &#91;10, 25, 30, 40, 50]\n\n# Modifying elements using negative indexing\nmy_list&#91;-2] = 45\nprint(my_list)          # Output: &#91;10, 25, 30, 45, 50]\n\n# Slicing using positive indexing\nprint(my_list&#91;1:3])     # Output: &#91;25, 30]\n\n# Slicing using negative indexing\nprint(my_list&#91;-3:-1])   # Output: &#91;30, 45]<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\">In summary, positive indexing starts from 0 and goes up to n-1, while negative indexing starts from -1 and goes down to -n. Positive indexing is used to access or modify elements, while negative indexing is used to access elements from the end of the list. Slicing can be done using both positive and negative indexing.<\/p>\n\n\n\n<p>Example : <strong>Accessing through Positive indexes<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-92.png\" alt=\"\" class=\"wp-image-25372\" width=\"449\" height=\"41\" srcset=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-92.png 577w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-92-300x28.png 300w\" sizes=\"auto, (max-width: 449px) 100vw, 449px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-93.png\" alt=\"\" class=\"wp-image-25374\" width=\"548\" height=\"314\" srcset=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-93.png 693w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-93-300x172.png 300w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-93-600x344.png 600w\" sizes=\"auto, (max-width: 548px) 100vw, 548px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-94.png\" alt=\"\" class=\"wp-image-25375\" width=\"669\" height=\"402\" srcset=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-94.png 921w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-94-300x180.png 300w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-94-768x462.png 768w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-94-500x300.png 500w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-94-760x457.png 760w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-94-600x361.png 600w\" sizes=\"auto, (max-width: 669px) 100vw, 669px\" \/><\/figure>\n\n\n\n<p>Example : <strong>Accessing through Negative indexes<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"307\" src=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-95.png\" alt=\"\" class=\"wp-image-25376\" srcset=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-95.png 1024w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-95-300x90.png 300w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-95-768x230.png 768w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-95-760x228.png 760w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-95-600x180.png 600w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"281\" src=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-96.png\" alt=\"\" class=\"wp-image-25377\" srcset=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-96.png 1024w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-96-300x82.png 300w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-96-768x211.png 768w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-96-760x209.png 760w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-96-600x165.png 600w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"has-background\" style=\"background-color:#d9e4e8;font-size:30px\"><strong>Index Error<\/strong><\/p>\n\n\n\n<p class=\"has-medium-font-size\">An Index Error occurs when you try to access an element in a list using an invalid index, which means the index is either negative, greater than or equal to the length of the list.<\/p>\n\n\n\n<p class=\"has-medium-font-size\">Here is an example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>my_list = &#91;'apple', 'banana', 'orange']\nprint(my_list&#91;3])<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\">In this example, the list <code>my_list<\/code> has three elements, with indices 0, 1, and 2. The above code tries to access the element at index 3, which is out of range and will result in an I<strong>ndex Error. <\/strong>The correct way to access the last element of this list would be to use index 2, like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>print(my_list&#91;2])<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\">This will print the last element of the list, which is &#8216;orange&#8217;.<\/p>\n\n\n\n<p>Example : <\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-97-1024x424.png\" alt=\"\" class=\"wp-image-25383\" width=\"663\" height=\"274\" srcset=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-97-1024x424.png 1024w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-97-300x124.png 300w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-97-768x318.png 768w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-97-760x315.png 760w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-97-600x248.png 600w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-97.png 1039w\" sizes=\"auto, (max-width: 663px) 100vw, 663px\" \/><\/figure>\n\n\n\n<p class=\"has-background has-medium-font-size\" style=\"background-color:#dfe8eb\"><strong>Traversing a list using for Loop<\/strong><\/p>\n\n\n\n<p class=\"has-medium-font-size\">The most common way to traverse the elements of a list is with a for loop. The syntax and example is as illustrated in the example below<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-98.png\" alt=\"\" class=\"wp-image-25391\" width=\"412\" height=\"247\" srcset=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-98.png 548w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-98-300x180.png 300w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-98-500x300.png 500w\" sizes=\"auto, (max-width: 412px) 100vw, 412px\" \/><\/figure>\n\n\n\n<p class=\"has-medium-font-size\">This loop traverses the list and updates each element.&nbsp;<strong><em>len<\/em><\/strong><em>&nbsp;returns the number of elements in the list<\/em>.&nbsp;<strong>range&nbsp;<\/strong>returns a list of indices from&nbsp;<strong><em>0 to n \u2212 1<\/em><\/strong>, where&nbsp;<strong><em>n<\/em><\/strong>&nbsp;is the length of the list.<\/p>\n\n\n\n<p class=\"has-medium-font-size\"><strong>Note 1 : A for loop over an empty list never executes the body<\/strong>.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-99.png\" alt=\"\" class=\"wp-image-25394\" width=\"370\" height=\"131\" srcset=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-99.png 461w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-99-300x106.png 300w\" sizes=\"auto, (max-width: 370px) 100vw, 370px\" \/><\/figure>\n\n\n\n<p class=\"has-medium-font-size\"><strong>Note 2 : Although a list can contain another list, the nested list still counts as a single element. The length of this list is four:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-100.png\" alt=\"\" class=\"wp-image-25395\" width=\"768\" height=\"143\" srcset=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-100.png 768w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-100-300x56.png 300w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-100-760x142.png 760w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-100-600x112.png 600w\" sizes=\"auto, (max-width: 768px) 100vw, 768px\" \/><\/figure>\n\n\n\n<p class=\"has-medium-font-size\">In Python, a nested list is a list that contains other lists as elements. In other words, a nested list is a list within a list. You can have multiple levels of nesting, which means a list can contain other lists, which in turn can contain more lists and so on.<\/p>\n\n\n\n<p class=\"has-medium-font-size\">Here is an example of a simple nested list:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>my_list = &#91;&#91;1, 2, 3], &#91;4, 5, 6], &#91;7, 8, 9]]<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\">In this example, <code>my_list<\/code> is a list that contains three other lists as its elements. Each of the inner lists contains three integers.<\/p>\n\n\n\n<p class=\"has-medium-font-size\">You can access the elements of a nested list using indexing. For example, to access the element at the first row and second column of <code>my_list<\/code>, you would use the following code:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>my_list&#91;0]&#91;1] # Output: 2<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\">Here, <code>my_list[0]<\/code> returns the first list in the nested list, and <code>my_list[0][1]<\/code> returns the second element of that list.<\/p>\n\n\n\n<p class=\"has-medium-font-size\">You can also use a nested loop to iterate over the elements of a nested list. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for row in my_list:\n    for item in row:\n        print(item)<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\">This code will print all the elements of<strong> <code>my_list<\/code> i<\/strong>n order, one by one.<\/p>\n\n\n\n<p style=\"font-size:30px\"><strong>List Slicing \/Getting Sublists with Slices<\/strong><\/p>\n\n\n\n<p class=\"has-medium-font-size\">In Python, list slicing is a way to extract a subset of elements from a list. Slicing allows you to extract a contiguous portion of a list, specified by its starting and ending indices. The syntax for slicing a list is as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>my_list&#91;start:end:step]<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\">Here, <code>start<\/code> is the index of the first element to include in the slice, <code>end<\/code> is the index of the first element to exclude from the slice, and <code>step<\/code> is the number of elements to skip between each element in the slice. Note that the <code>end<\/code> index is exclusive, which means that the element at the <code>end<\/code> index is not included in the slice.<\/p>\n\n\n\n<p class=\"has-medium-font-size\">Here are some examples of list slicing:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>my_list = &#91;1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n\n# Extract a slice of the first 5 elements\nslice1 = my_list&#91;0:5]\nprint(slice1) # Output: &#91;1, 2, 3, 4, 5]\n\n# Extract a slice of the elements at odd indices\nslice2 = my_list&#91;1::2]\nprint(slice2) # Output: &#91;2, 4, 6, 8, 10]\n\n# Extract a slice of the elements in reverse order\nslice3 = my_list&#91;::-1]\nprint(slice3) # Output: &#91;10, 9, 8, 7, 6, 5, 4, 3, 2, 1]<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\">In the first example, we extract a slice of the first 5 elements of <code>my_list<\/code>. In the second example, we extract a slice of the elements at odd indices, starting from the second element. In the third example, we extract a slice of all the elements in reverse order.<\/p>\n\n\n\n<p class=\"has-medium-font-size\">Note that you can also use negative indices for <code>start<\/code> and <code>end<\/code>. In this case, the index is counted from the end of the list, with <code>-1<\/code> being the index of the last element. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Extract a slice of the last 3 elements\nslice4 = my_list&#91;-3:]\nprint(slice4) # Output: &#91;8, 9, 10]<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\">This example extracts a slice of the last 3 elements of <code>my_list<\/code>.<\/p>\n\n\n\n<p class=\"has-medium-font-size\"><strong>Examples :<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-101.png\" alt=\"\" class=\"wp-image-25407\" width=\"374\" height=\"241\" srcset=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-101.png 474w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-101-300x194.png 300w\" sizes=\"auto, (max-width: 374px) 100vw, 374px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-102.png\" alt=\"\" class=\"wp-image-25409\" width=\"422\" height=\"457\" srcset=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-102.png 582w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-102-277x300.png 277w\" sizes=\"auto, (max-width: 422px) 100vw, 422px\" \/><\/figure>\n\n\n\n<p class=\"has-medium-font-size\"><strong>Usage of in and not in membership operators in list<\/strong><\/p>\n\n\n\n<p class=\"has-medium-font-size\">In Python, the <code>in<\/code> and <code>not in<\/code> operators are used to check if an element is present in a list or not. These operators return a boolean value (<code>True<\/code> or <code>False<\/code>) depending on whether the element is found in the list or not.<\/p>\n\n\n\n<p class=\"has-medium-font-size\">The <code>in<\/code> operator checks if an element is present in a list, and returns <code>True<\/code> if it is, and <code>False<\/code> otherwise. Here is an example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>my_list = &#91;'apple', 'banana', 'orange']\nprint('banana' in my_list) # Output: True\nprint('grape' in my_list) # Output: False<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\">In this example, the first print statement checks if the string &#8216;banana&#8217; is present in the list <code>my_list<\/code>, which it is, so it returns <code>True<\/code>. The second print statement checks if the string &#8216;grape&#8217; is present in the list <code>my_list<\/code>, which it is not, so it returns <code>False<\/code>.<\/p>\n\n\n\n<p class=\"has-medium-font-size\">The <code>not in<\/code> operator does the opposite of <code>in<\/code>. It checks if an element is not present in a list, and returns <code>True<\/code> if it is not, and <code>False<\/code> otherwise. Here is an example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>my_list = &#91;'apple', 'banana', 'orange']\nprint('banana' not in my_list) # Output: False\nprint('grape' not in my_list) # Output: <\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\">In this example, the first print statement checks if the string &#8216;banana&#8217; is not present in the list <code>my_list<\/code>, which it is, so it returns <code>False<\/code>. The second print statement checks if the string &#8216;grape&#8217; is not present in the list <code>my_list<\/code>, which it is not, so it returns <code>True<\/code>.<\/p>\n\n\n\n<p class=\"has-medium-font-size\">The <code>in<\/code> and <code>not in<\/code> operators can be used with other data types as well, such as strings, tuples, and sets, in addition to lists.<\/p>\n\n\n\n<p class=\"has-background has-medium-font-size\" style=\"background-color:#e0e7ea\"><strong>Multiple Assignment<\/strong><\/p>\n\n\n\n<p class=\"has-medium-font-size\">In Python, multiple assignment is a feature that allows you to assign multiple values to multiple variables in a single statement. This can be useful when you have multiple values that you want to assign to multiple variables without having to write separate statements for each assignment.<\/p>\n\n\n\n<p class=\"has-medium-font-size\">The syntax for multiple assignment is as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>a, b, c = 1, 2, 3<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\">In this example, the values <code>1<\/code>, <code>2<\/code>, and <code>3<\/code> are assigned to the variables <code>a<\/code>, <code>b<\/code>, and <code>c<\/code>, respectively. This is equivalent to the following three separate assignment statements:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>a = 1\nb = 2\nc = 3<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\">You can also use multiple assignment to swap the values of two variables without using a temporary variable, like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>a, b = b, a<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\">In this example, the values of <code>a<\/code> and <code>b<\/code> are swapped.<\/p>\n\n\n\n<p class=\"has-medium-font-size\">Multiple assignment can also be used with lists, tuples, or any other iterable object. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>my_list = &#91;1, 2, 3]\na, b, c = my_list<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\">In this example, the values of <code>my_list<\/code> are unpacked and assigned to the variables <code>a<\/code>, <code>b<\/code>, and <code>c<\/code>, respectively.<\/p>\n\n\n\n<p class=\"has-medium-font-size\">Note that the number of variables on the left-hand side of the assignment must match the number of values on the right-hand side of the assignment, otherwise you will get a <code>ValueError<\/code> exception.<\/p>\n\n\n\n<p class=\"has-medium-font-size\"><strong>Example :<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-103.png\" alt=\"\" class=\"wp-image-25416\" width=\"532\" height=\"129\" srcset=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-103.png 683w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-103-300x73.png 300w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-103-600x146.png 600w\" sizes=\"auto, (max-width: 532px) 100vw, 532px\" \/><\/figure>\n\n\n\n<p class=\"has-medium-font-size\"><strong>Example : <\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-104.png\" alt=\"\" class=\"wp-image-25418\" width=\"667\" height=\"295\" srcset=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-104.png 934w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-104-300x133.png 300w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-104-768x340.png 768w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-104-760x336.png 760w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-104-600x265.png 600w\" sizes=\"auto, (max-width: 667px) 100vw, 667px\" \/><\/figure>\n\n\n\n<p class=\"has-medium-font-size\"><strong>Example :<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-105.png\" alt=\"\" class=\"wp-image-25422\" width=\"657\" height=\"208\" srcset=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-105.png 947w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-105-300x95.png 300w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-105-768x244.png 768w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-105-760x242.png 760w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-105-600x191.png 600w\" sizes=\"auto, (max-width: 657px) 100vw, 657px\" \/><\/figure>\n\n\n\n<p class=\"has-background\" style=\"background-color:#e3ebef;font-size:30px\"><strong>List Methods <\/strong><\/p>\n\n\n\n<p class=\"has-medium-font-size\">The list data type has several useful methods for <strong>finding , adding removing and otherwise<\/strong> manipulating values in a list. Some of the list methods are :<\/p>\n\n\n\n<ol class=\"has-medium-font-size wp-block-list\"><li><code><strong>append()<\/strong><\/code> &#8211; Adds an element to the end of the list<\/li><li><code><strong>extend()<\/strong><\/code> &#8211; Adds one or more elements of an iterable to the end of the list<\/li><li><strong><code>insert()<\/code> <\/strong>&#8211; Inserts an element at a specified position in the list<\/li><li><code><strong>remove()<\/strong><\/code> &#8211; Removes the first occurrence of an element from the list<\/li><li><strong><code>pop()<\/code> &#8211;<\/strong> Removes and returns the element at a specified position in the list<\/li><li><code><strong>index()<\/strong><\/code> &#8211; Returns the index of the first occurrence of an element in the list<\/li><li><code><strong>count()<\/strong><\/code> &#8211; Returns the number of times an element appears in the list<\/li><li><strong><code>sort()<\/code> <\/strong>&#8211; Sorts the elements of the list in ascending order<\/li><li><strong><code>reverse()<\/code> <\/strong>&#8211; Reverses the order of the elements in the list<\/li><li><code><strong>copy()<\/strong><\/code> &#8211; Returns a shallow copy of the list<\/li><li><strong><code>clear()<\/code> <\/strong>&#8211; Removes all elements from the list<\/li><\/ol>\n\n\n\n<p class=\"has-background has-medium-font-size\" style=\"background-color:#dae2e5\"><strong>append()<\/strong><\/p>\n\n\n\n<p class=\"has-medium-font-size\"><code>append()<\/code> is a built-in method in Python&#8217;s list class that adds a new element to the end of the list. The syntax for using <code>append()<\/code> is straightforward:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>list_name.append(element)<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\">Here, <code>list_name<\/code> refers to the name of the list you want to modify, and <code>element<\/code> is the value you want to append to the list.<\/p>\n\n\n\n<p class=\"has-medium-font-size\">Here&#8217;s an example that demonstrates the use of <code>append()<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>my_list = &#91;1, 2, 3]\nmy_list.append(4)\nprint(my_list)  # Output: &#91;1, 2, 3, 4]<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\">In this example, we start with a list <code>[1, 2, 3]<\/code>, and then use <code>append()<\/code> to add the value <code>4<\/code> to the end of the list. The output shows that the list now contains the elements <code>[1, 2, 3, 4]<\/code>.<\/p>\n\n\n\n<p class=\"has-medium-font-size\">Note that <code>append()<\/code> only adds a single element to the end of the list. If you want to add multiple elements to a list, you can use the <code>extend()<\/code> method instead.<\/p>\n\n\n\n<p class=\"has-medium-font-size\">Also, it&#8217;s worth noting that <code>append()<\/code> modifies the original list in place, rather than creating a new list. This means that any references to the original list will also reflect the changes made by <code>append()<\/code>.<\/p>\n\n\n\n<p class=\"has-medium-font-size\"><strong>Example : <\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-119.png\" alt=\"\" class=\"wp-image-25513\" width=\"451\" height=\"349\" srcset=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-119.png 567w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-119-300x232.png 300w\" sizes=\"auto, (max-width: 451px) 100vw, 451px\" \/><\/figure>\n\n\n\n<p class=\"has-background has-medium-font-size\" style=\"background-color:#dbe4e8\"><strong>extend()<\/strong><\/p>\n\n\n\n<p class=\"has-medium-font-size\"><code>extend()<\/code> is a built-in method in Python&#8217;s list class that appends the elements of an iterable to the end of an existing list. The syntax for using <code>extend()<\/code> is as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>list_name.extend(iterable)<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\">Here, <code>list_name<\/code> refers to the name of the list you want to modify, and <code>iterable<\/code> is any iterable object, such as a list, tuple, or string, that contains the elements you want to add to the end of the list.<\/p>\n\n\n\n<p class=\"has-medium-font-size\">Here&#8217;s an example that demonstrates the use of <code>extend()<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>my_list = &#91;1, 2, 3]\nmy_list.extend(&#91;4, 5, 6])\nprint(my_list)  # Output: &#91;1, 2, 3, 4, 5, 6]<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\">In this example, we start with a list <code>[1, 2, 3]<\/code>, and then use <code>extend()<\/code> to add the elements <code>[4, 5, 6]<\/code> to the end of the list. The output shows that the list now contains the elements <code>[1, 2, 3, 4, 5, 6]<\/code>.<\/p>\n\n\n\n<p class=\"has-medium-font-size\">Note that <code>extend()<\/code> can add multiple elements to a list at once, and can be used to extend a list with elements from another list or any other iterable object.<\/p>\n\n\n\n<p class=\"has-medium-font-size\">It&#8217;s also worth noting that <code>extend()<\/code> modifies the original list in place, rather than creating a new list. This means that any references to the original list will also reflect the changes made by <code>extend()<\/code>.<\/p>\n\n\n\n<p class=\"has-background has-medium-font-size\" style=\"background-color:#d7dee1\"><strong>insert()<\/strong><\/p>\n\n\n\n<p class=\"has-medium-font-size\"><code>insert()<\/code> is a built-in method in Python&#8217;s list class that allows you to insert a new element at a specific position in a list. The syntax for using <code>insert()<\/code> is as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>list_name.insert(index, element)<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\">Here, <code>list_name<\/code> refers to the name of the list you want to modify, <code>index<\/code> is the position in the list where you want to insert the new element, and <code>element<\/code> is the value you want to insert.<\/p>\n\n\n\n<p class=\"has-medium-font-size\">Here&#8217;s an example that demonstrates the use of <code>insert()<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>my_list = &#91;1, 2, 3]\nmy_list.insert(1, 4)\nprint(my_list)  # Output: &#91;1, 4, 2, 3]<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\">In this example, we start with a list <code>[1, 2, 3]<\/code>, and then use <code>insert()<\/code> to add the value <code>4<\/code> at index 1 (i.e., between the values 1 and 2) in the list. The output shows that the list now contains the elements <code>[1, 4, 2, 3]<\/code>.<\/p>\n\n\n\n<p class=\"has-medium-font-size\">Note that <code>insert()<\/code> modifies the original list in place, rather than creating a new list. This means that any references to the original list will also reflect the changes made by <code>insert()<\/code>. Also, if you try to insert an element at an index that is outside the range of the list, Python will raise an <code>IndexError<\/code> exception.<\/p>\n\n\n\n<p>Example : <\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-120.png\" alt=\"\" class=\"wp-image-25515\" width=\"659\" height=\"238\" srcset=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-120.png 783w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-120-300x108.png 300w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-120-768x278.png 768w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-120-760x275.png 760w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-120-600x217.png 600w\" sizes=\"auto, (max-width: 659px) 100vw, 659px\" \/><\/figure>\n\n\n\n<p class=\"has-background has-medium-font-size\" style=\"background-color:#ecf0f2\"><strong>remove()<\/strong><\/p>\n\n\n\n<p class=\"has-medium-font-size\">The <code>remove()<\/code> method is a built-in function in Python lists that is used to remove the first occurrence of a specified value from the list. It takes one argument, which is the value to be removed from the list.<\/p>\n\n\n\n<p class=\"has-medium-font-size\">Here is the syntax for the <code>remove()<\/code> method:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>list.remove(value)<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\">where <code>list<\/code> is the name of the list and <code>value<\/code> is the value to be removed from the list.<\/p>\n\n\n\n<p class=\"has-medium-font-size\">Examples:<\/p>\n\n\n\n<ol class=\"has-medium-font-size wp-block-list\"><li>Removing an element from a list<\/li><\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>fruits = &#91;\"apple\", \"banana\", \"cherry\", \"banana\"]\nfruits.remove(\"banana\")\nprint(fruits)  # Output: &#91;\"apple\", \"cherry\", \"banana\"]<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\">In the above example, the first occurrence of &#8220;banana&#8221; is removed from the list.<\/p>\n\n\n\n<ol start=\"2\" class=\"has-medium-font-size wp-block-list\"><li>Removing an element that is not present in the list<\/li><\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>fruits = &#91;\"apple\", \"banana\", \"cherry\"]\nfruits.remove(\"mango\") # Raises ValueError: list.remove(x): x not in list<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\">In the above example, since &#8220;mango&#8221; is not present in the list, <code>remove()<\/code> method raises a ValueError.<\/p>\n\n\n\n<p class=\"has-medium-font-size\">Note that if there are multiple occurrences of the specified value in the list, only the first occurrence will be removed. If the value is not present in the list, a ValueError will be raised.<\/p>\n\n\n\n<p>Example : <\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-121.png\" alt=\"\" class=\"wp-image-25518\" width=\"555\" height=\"112\" srcset=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-121.png 803w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-121-300x61.png 300w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-121-768x155.png 768w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-121-760x153.png 760w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-121-600x121.png 600w\" sizes=\"auto, (max-width: 555px) 100vw, 555px\" \/><\/figure>\n\n\n\n<p class=\"has-background has-medium-font-size\" style=\"background-color:#eaeef0\"><strong>pop()<\/strong><\/p>\n\n\n\n<p class=\"has-medium-font-size\">The <code>pop()<\/code> method in Python is a built-in function in lists that removes and returns the element at the specified index. If no index is specified, it removes and returns the last element in the list.<\/p>\n\n\n\n<p class=\"has-medium-font-size\">Here is the syntax for the <code>pop()<\/code> method:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>list.pop(&#91;index])<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\">where <code>list<\/code> is the name of the list and <code>index<\/code> is the index of the element to be removed. The <code>index<\/code> parameter is optional. If it is not specified, then <code>pop()<\/code> removes and returns the last element of the list.<\/p>\n\n\n\n<p class=\"has-medium-font-size\">Examples:<\/p>\n\n\n\n<ol class=\"has-medium-font-size wp-block-list\"><li>Removing the last element of a list:<\/li><\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>fruits = &#91;\"apple\", \"banana\", \"cherry\"]\nlast_fruit = fruits.pop()\nprint(last_fruit)  # Output: \"cherry\"\nprint(fruits)  # Output: &#91;\"apple\", \"banana\"]<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\">In the above example, <code>pop()<\/code> removes and returns the last element &#8220;cherry&#8221; from the list. The <code>last_fruit<\/code> variable stores the returned value. The updated list after removing the last element is <code>[\"apple\", \"banana\"]<\/code>.<\/p>\n\n\n\n<p class=\"has-medium-font-size\">2. Removing an element at a specific index:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>fruits = &#91;\"apple\", \"banana\", \"cherry\"]\nsecond_fruit = fruits.pop(1)\nprint(second_fruit)  # Output: \"banana\"\nprint(fruits)  # Output: &#91;\"apple\", \"cherry\"]<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\">In the above example, <code>pop(1)<\/code> removes and returns the element at index 1, which is &#8220;banana&#8221;. The <code>second_fruit<\/code> variable stores the returned value. The updated list after removing the element at index 1 is <code>[\"apple\", \"cherry\"]<\/code>.<\/p>\n\n\n\n<ol start=\"3\" class=\"has-medium-font-size wp-block-list\"><li>Removing an element from an empty list:<\/li><\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>fruits = &#91;]\nfruit = fruits.pop()  # Raises IndexError: pop from empty list<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\">In the above example, since the list <code>fruits<\/code> is empty, calling <code>pop()<\/code> method raises an IndexError because there is no element to remove from the list.<\/p>\n\n\n\n<p>Example : <\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-124.png\" alt=\"\" class=\"wp-image-25525\" width=\"230\" height=\"151\" srcset=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-124.png 318w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-124-300x197.png 300w\" sizes=\"auto, (max-width: 230px) 100vw, 230px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p class=\"has-background has-medium-font-size\" style=\"background-color:#e1ebef\"><strong>index() <\/strong><\/p>\n\n\n\n<p class=\"has-medium-font-size\">The <code>index()<\/code> method is a built-in function in Python lists that returns the index of the first occurrence of a specified element in the list. If the specified element is not in the list, it raises a ValueError.<\/p>\n\n\n\n<p class=\"has-medium-font-size\">Here is the syntax for the <code>index()<\/code> method:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>list.index(element&#91;, start&#91;, end]])<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\">where <code>list<\/code> is the name of the list, <code>element<\/code> is the element to be searched, <code>start<\/code> (optional) is the starting index of the search, and <code>end<\/code> (optional) is the ending index of the search.<\/p>\n\n\n\n<p class=\"has-medium-font-size\">Examples:<\/p>\n\n\n\n<ol class=\"has-medium-font-size wp-block-list\"><li>Finding the index of an element:<\/li><\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>fruits = &#91;\"apple\", \"banana\", \"cherry\"]\nbanana_index = fruits.index(\"banana\")\nprint(banana_index)  # Output: 1<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\">In the above example, <code>index()<\/code> method returns the index of the first occurrence of &#8220;banana&#8221; in the list, which is 1.<\/p>\n\n\n\n<ol start=\"2\" class=\"has-medium-font-size wp-block-list\"><li>Finding the index of an element within a specified range:<\/li><\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>fruits = &#91;\"apple\", \"banana\", \"cherry\", \"banana\", \"orange\"]\nbanana_index = fruits.index(\"banana\", 2, 4)\nprint(banana_index)  # Output: 3<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\">In the above example, <code>index(\"banana\", 2, 4)<\/code> searches for the first occurrence of &#8220;banana&#8221; between the indices 2 and 4 (exclusive). Since the first occurrence of &#8220;banana&#8221; within this range is at index 3, that value is returned.<\/p>\n\n\n\n<ol start=\"3\" class=\"has-medium-font-size wp-block-list\"><li>Finding the index of an element that is not in the list:<\/li><\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>fruits = &#91;\"apple\", \"banana\", \"cherry\"]\nmango_index = fruits.index(\"mango\")  # Raises ValueError: 'mango' is not in list<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\">In the above example, since &#8220;mango&#8221; is not in the list, calling <code>index()<\/code> method raises a ValueError.<\/p>\n\n\n\n<p>Example : <\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-118.png\" alt=\"\" class=\"wp-image-25511\" width=\"341\" height=\"155\" srcset=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-118.png 483w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-118-300x137.png 300w\" sizes=\"auto, (max-width: 341px) 100vw, 341px\" \/><\/figure>\n\n\n\n<p class=\"has-background has-medium-font-size\" style=\"background-color:#e3e9eb\"><strong>count ()<\/strong><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p class=\"has-medium-font-size\">The <code>count()<\/code> method is a built-in function in Python lists that returns the number of times a specified element appears in the list.<\/p>\n\n\n\n<p class=\"has-medium-font-size\">Here is the syntax for the <code>count()<\/code> method:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>list.count(element)<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\">where <code>list<\/code> is the name of the list and <code>element<\/code> is the element to be counted.<\/p>\n\n\n\n<p class=\"has-medium-font-size\">Examples:<\/p>\n\n\n\n<ol class=\"has-medium-font-size wp-block-list\"><li>Counting the number of occurrences of an element:<\/li><\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>fruits = &#91;\"apple\", \"banana\", \"cherry\", \"banana\", \"orange\"]\nbanana_count = fruits.count(\"banana\")\nprint(banana_count)  # Output: 2<\/code><\/pre>\n\n\n\n<p>In the above example, <code>count()<\/code> method returns the number of occurrences of &#8220;banana&#8221; in the list, which is 2.<\/p>\n\n\n\n<p class=\"has-medium-font-size\">2. Counting the number of occurrences of an element that is not in the list:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>fruits = &#91;\"apple\", \"banana\", \"cherry\"]\nmango_count = fruits.count(\"mango\")\nprint(mango_count)  # Output: 0<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\">In the above example, <code>count()<\/code> method returns 0 since &#8220;mango&#8221; is not present in the list.<\/p>\n\n\n\n<ol start=\"3\" class=\"has-medium-font-size wp-block-list\"><li>Counting the number of occurrences of an element in an empty list:<\/li><\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>fruits = &#91;]\napple_count = fruits.count(\"apple\")\nprint(apple_count)  # Output: 0<\/code><\/pre>\n\n\n\n<p>In the above example, <code>count()<\/code> method returns 0 since the list is empty and &#8220;apple&#8221; is not present in the list.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p class=\"has-background has-medium-font-size\" style=\"background-color:#e2e9eb\"><strong>sort() method <\/strong><\/p>\n\n\n\n<p class=\"has-medium-font-size\">The <code>sort()<\/code> method is a built-in function in Python lists that sorts the elements of a list in ascending or descending order.<\/p>\n\n\n\n<p class=\"has-medium-font-size\">Here is the syntax for the <code>sort()<\/code> method:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>list.sort(key=None, reverse=False)<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\">where <code>list<\/code> is the name of the list, <code>key<\/code> (optional) is a function that specifies the key to sort by, and <code>reverse<\/code> (optional) is a boolean value that determines whether to sort the list in descending order (True) or ascending order (False). The default value of <code>key<\/code> is None, which means the elements are compared directly. The default value of <code>reverse<\/code> is False.<\/p>\n\n\n\n<p class=\"has-medium-font-size\">Examples:<\/p>\n\n\n\n<ol class=\"has-medium-font-size wp-block-list\"><li><strong>Sorting a list in ascending order:<\/strong><\/li><\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>fruits = &#91;\"banana\", \"apple\", \"cherry\", \"orange\"]\nfruits.sort()\nprint(fruits)  # Output: &#91;\"apple\", \"banana\", \"cherry\", \"orange\"]<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\">In the above example, <code>sort()<\/code> method sorts the elements of the <code>fruits<\/code> list in ascending order.<\/p>\n\n\n\n<ol start=\"2\" class=\"has-medium-font-size wp-block-list\"><li><strong>Sorting a list in descending order:<\/strong><\/li><\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>fruits = &#91;\"banana\", \"apple\", \"cherry\", \"orange\"]\nfruits.sort(reverse=True)\nprint(fruits)  # Output: &#91;\"orange\", \"cherry\", \"banana\", \"apple\"]<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\">In the above example, <code>sort(reverse=True)<\/code> sorts the elements of the <code>fruits<\/code> list in descending order.<\/p>\n\n\n\n<ol start=\"3\" class=\"has-medium-font-size wp-block-list\"><li><strong>Sorting a list of tuples by a specific element:<\/strong><\/li><\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>students = &#91;(\"Alice\", 25), (\"Bob\", 18), (\"Charlie\", 22), (\"Dave\", 30)]\nstudents.sort(key=lambda x: x&#91;1])\nprint(students)  # Output: &#91;(\"Bob\", 18), (\"Charlie\", 22), (\"Alice\", 25), (\"Dave\", 30)]<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\">In the above example, <code>sort(key=lambda x: x[1])<\/code> sorts the <code>students<\/code> list of tuples by the second element (i.e., age) in ascending order. The lambda function <code>lambda x: x[1]<\/code> specifies the key to sort by.<\/p>\n\n\n\n<p class=\"has-medium-font-size\">Note: The <code>sort()<\/code> method modifies the original list and does not return a new list. If you want to sort a list without modifying the original list, you can use the <code>sorted()<\/code> function instead.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p class=\"has-medium-font-size\"><strong>Example : <\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-125.png\" alt=\"\" class=\"wp-image-25526\" width=\"684\" height=\"294\" srcset=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-125.png 762w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-125-300x129.png 300w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-125-760x327.png 760w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-125-600x258.png 600w\" sizes=\"auto, (max-width: 684px) 100vw, 684px\" \/><\/figure>\n\n\n\n<p class=\"has-medium-font-size\"><strong>Example : <\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-126.png\" alt=\"\" class=\"wp-image-25528\" width=\"574\" height=\"222\" srcset=\"https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-126.png 727w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-126-300x116.png 300w, https:\/\/tocxten.com\/wp-content\/uploads\/2023\/03\/image-126-600x232.png 600w\" sizes=\"auto, (max-width: 574px) 100vw, 574px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p class=\"has-background has-medium-font-size\" style=\"background-color:#d9dfe1\"><strong>reverse() method <\/strong><\/p>\n\n\n\n<p class=\"has-medium-font-size\">The <code>reverse()<\/code> method is a built-in function in Python lists that reverses the order of the elements in a list.<\/p>\n\n\n\n<p class=\"has-medium-font-size\">Here is the syntax for the <code>reverse()<\/code> method:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>list.reverse()<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\">where <code>list<\/code> is the name of the list.<\/p>\n\n\n\n<p class=\"has-medium-font-size\">Examples:<\/p>\n\n\n\n<ol class=\"has-medium-font-size wp-block-list\"><li><strong>Reversing a list:<\/strong><\/li><\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>fruits = &#91;\"apple\", \"banana\", \"cherry\", \"orange\"]\nfruits.reverse()\nprint(fruits)  # Output: &#91;\"orange\", \"cherry\", \"banana\", \"apple\"]<\/code><\/pre>\n\n\n\n<p>In the above example, <code>reverse()<\/code> method reverses the order of the elements in the <code>fruits<\/code> list.<\/p>\n\n\n\n<ol start=\"2\" class=\"has-medium-font-size wp-block-list\"><li><strong>Reversing an empty list:<\/strong><\/li><\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>fruits = &#91;]\nfruits.reverse()\nprint(fruits)  # Output: &#91;]<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\">In the above example, <code>reverse()<\/code> method does not modify an empty list because there are no elements to reverse.<\/p>\n\n\n\n<p class=\"has-medium-font-size\">Note: The <code>reverse()<\/code> method modifies the original list and does not return a new list. If you want to reverse a list without modifying the original list, you can use the slicing operator <code>[::-1]<\/code> instead.<\/p>\n\n\n\n<p class=\"has-background has-medium-font-size\" style=\"background-color:#e9eef0\"><strong>copy() method <\/strong><\/p>\n\n\n\n<p class=\"has-medium-font-size\">The <code>copy()<\/code> method is a built-in function in Python lists that returns a shallow copy of the list.<\/p>\n\n\n\n<p class=\"has-medium-font-size\">Here is the syntax for the <code>copy()<\/code> method:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>new_list = list.copy()<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\">where <code>list<\/code> is the name of the list that you want to copy and <code>new_list<\/code> is the new list that is created as a copy of the original list.<\/p>\n\n\n\n<p class=\"has-medium-font-size\">Examples:<\/p>\n\n\n\n<ol class=\"has-medium-font-size wp-block-list\"><li><strong>Creating a shallow copy of a list:<\/strong><\/li><\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>fruits = &#91;\"apple\", \"banana\", \"cherry\", \"orange\"]\nfruits_copy = fruits.copy()\nprint(fruits_copy)  # Output: &#91;\"apple\", \"banana\", \"cherry\", \"orange\"]<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\">In the above example, <code>copy()<\/code> method creates a shallow copy of the <code>fruits<\/code> list and assigns it to <code>fruits_copy<\/code>.<\/p>\n\n\n\n<ol start=\"2\" class=\"has-medium-font-size wp-block-list\"><li><strong>Modifying the original list does not affect the copied list:<\/strong><\/li><\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>fruits = &#91;\"apple\", \"banana\", \"cherry\", \"orange\"]\nfruits_copy = fruits.copy()\nfruits.pop()\nprint(fruits)  # Output: &#91;\"apple\", \"banana\", \"cherry\"]\nprint(fruits_copy)  # Output: &#91;\"apple\", \"banana\", \"cherry\", \"orange\"]<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\">In the above example, <code>copy()<\/code> method creates a shallow copy of the <code>fruits<\/code> list and assigns it to <code>fruits_copy<\/code>. The <code>pop()<\/code> method removes the last element of the <code>fruits<\/code> list. However, the <code>fruits_copy<\/code> list remains unchanged because it is a copy of the original <code>fruits<\/code> list.<\/p>\n\n\n\n<p class=\"has-background has-medium-font-size\" style=\"background-color:#d8e0e3\"><strong>clear() <\/strong><\/p>\n\n\n\n<p class=\"has-medium-font-size\">The <code>clear()<\/code> method is a built-in function in Python lists that removes all elements from the list. Here is the syntax for the <code>clear()<\/code> method:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>list.clear()<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\">where <code>list<\/code> is the name of the list that you want to clear.<\/p>\n\n\n\n<p class=\"has-medium-font-size\">Examples:<\/p>\n\n\n\n<ol class=\"has-medium-font-size wp-block-list\"><li><strong>Clearing a list:<\/strong><\/li><\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>fruits = &#91;\"apple\", \"banana\", \"cherry\", \"orange\"]\nfruits.clear()\nprint(fruits)  # Output: &#91;]<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\">In the above example, <code>clear()<\/code> method removes all elements from the <code>fruits<\/code> list.<\/p>\n\n\n\n<ol start=\"2\" class=\"has-medium-font-size wp-block-list\"><li><strong>Clearing an empty list:<\/strong><\/li><\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>fruits = &#91;]\nfruits.clear()\nprint(fruits)  # Output: &#91;]<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\">In the above example, <code>clear()<\/code> method does not modify an empty list because there are no elements to remove.<\/p>\n\n\n\n<p class=\"has-background\" style=\"background-color:#ecf1f4;font-size:30px\"><strong>Lists and Functions<\/strong><\/p>\n\n\n\n<p class=\"has-medium-font-size\">There are a number of built in functions that can be used on lists  that allow you to quickly look through a list without writing your own loops. Some of the functions used with lists are&nbsp;<strong>len(), max(), min(),sum(),etc<\/strong>. The sum() function only works when the list elements are number . The other functions&nbsp;<strong>max(), len(),etc<\/strong>., work with lists of strings and other types that can be comparable.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># len(): Returns the length of a list.\nmy_list = &#91;1, 2, 3, 4, 5]\nprint(len(my_list))  # Output: 5\n\n# min(): Returns the minimum value of a list (only if all elements are of the same type).\nmy_list = &#91;5, 2, 8, 1, 9]\nprint(min(my_list))  # Output: 1\n\n#max(): Returns the maximum value of a list (only if all elements are of the same type).\nmy_list = &#91;5, 2, 8, 1, 9]\nprint(max(my_list))  # Output: 9\n\n# sum(): Returns the sum of all elements in a list (only if all elements are numeric).\nmy_list = &#91;1, 2, 3, 4, 5]\nprint(sum(my_list))  # Output: 15\n\n# sorted(): Returns a sorted list.\nmy_list = &#91;5, 2, 8, 1, 9]\nsorted_list = sorted(my_list)\nprint(sorted_list)  # Output: &#91;1, 2, 5, 8, 9]\n\n# reversed(): Returns a reversed list.\nmy_list = &#91;1, 2, 3, 4, 5]\nreversed_list = list(reversed(my_list))\nprint(reversed_list)  # Output: &#91;5, 4, 3, 2, 1]\n\n# list(): Converts an iterable object into a list.\nmy_tuple = (1, 2, 3, 4, 5)\nmy_list = list(my_tuple)\nprint(my_list)  # Output: &#91;1, 2, 3, 4, 5]<\/code><\/pre>\n\n\n\n<p class=\"has-background\" style=\"background-color:#e3eaed;font-size:24px\"><strong>Python program to compute average of numbers without using  list.<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Initialize variables\nsum_of_numbers = 0\ncount_of_numbers = 0\n\n# Keep taking input until the user enters 'done'\nwhile True:\n    num = input(\"Enter a number (or 'done' to exit): \")\n    if num == 'done':\n        break\n    sum_of_numbers += int(num)\n    count_of_numbers += 1\n\n# Compute the average\nif count_of_numbers &gt; 0:\n    average = sum_of_numbers \/ count_of_numbers\n    print(\"Average:\", average)\nelse:\n    print(\"No numbers entered.\")<\/code><\/pre>\n\n\n\n<p class=\"has-background\" style=\"background-color:#e5ecef;font-size:24px\"><strong>Python program to compute average of numbers  using list.<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Take input from the user and store it in a list\nnumbers = &#91;]\nwhile True:\n    num = input(\"Enter a number (or 'done' to exit): \")\n    if num == 'done':\n        break\n    numbers.append(int(num))\n\n# Compute the average\nif len(numbers) &gt; 0:\n    average = sum(numbers) \/ len(numbers)\n    print(\"Average:\", average)\nelse:\n    print(\"No numbers entered.\")<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\"><strong>Example : For a given list num = [45,22,14,65,97,72] write a python program to replace all the integers divisible by 3 with\u201dppp\u201d and all integers divisible by 5 with \u201cqqq\u201d and replace all the integers divisible by both 3 and 5 with\u201d pppqqq\u201d and display the output.<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>num = &#91;45, 22, 14, 65, 97, 72]\n\n# Iterate over the list and replace the numbers\nfor i in range(len(num)):\n    if num&#91;i] % 3 == 0 and num&#91;i] % 5 == 0:\n        num&#91;i] = \"pppqqq\"\n    elif num&#91;i] % 3 == 0:\n        num&#91;i] = \"ppp\"\n    elif num&#91;i] % 5 == 0:\n        num&#91;i] = \"qqq\"\n\n# Print the modified list\nprint(num)<\/code><\/pre>\n\n\n\n<p><strong>Output :<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;'pppqqq', 22, 14, 'qqq', 97, 'ppp']<\/code><\/pre>\n\n\n\n<p class=\"has-background\" style=\"background-color:#d7e2e6;font-size:30px\"><strong>Copy and Deep Copy<\/strong><\/p>\n\n\n\n<p class=\"has-medium-font-size\">In Python, <code>copy()<\/code> and <code>deepcopy()<\/code> are two methods that can be used to create copies of lists (and other objects). The main difference between them is how they handle nested objects (e.g., lists or dictionaries) inside the list being copied.<\/p>\n\n\n\n<p class=\"has-medium-font-size\">The <code>copy()<\/code> method creates a new list that is a shallow copy of the original list. This means that a new list object is created, but the elements of the new list are references to the same objects as the original list. If the original list contains mutable objects (e.g., lists or dictionaries), changes to these objects in the original list will be reflected in the copied list as well.<\/p>\n\n\n\n<p class=\"has-medium-font-size\">On the other hand, the <code>deepcopy()<\/code> method creates a completely new list with new objects. This means that even if the original list contains mutable objects, changes to these objects in the original list will not be reflected in the copied list.<\/p>\n\n\n\n<p class=\"has-medium-font-size\">Here are some examples that illustrate the differences between <code>copy()<\/code> and <code>deepcopy()<\/code>:<\/p>\n\n\n\n<ol class=\"has-medium-font-size wp-block-list\"><li><strong>Shallow copy using <code>copy()<\/code> method:<\/strong><\/li><\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>list1 = &#91;&#91;1, 2], &#91;3, 4]]\nlist2 = list1.copy()\n\nprint(list1)  # Output: &#91;&#91;1, 2], &#91;3, 4]]\nprint(list2)  # Output: &#91;&#91;1, 2], &#91;3, 4]]\n\nlist1&#91;0]&#91;0] = 5\n\nprint(list1)  # Output: &#91;&#91;5, 2], &#91;3, 4]]\nprint(list2)  # Output: &#91;&#91;5, 2], &#91;3, 4]]<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\">In the above example, <code>list1<\/code> is a nested list with two inner lists. <code>list2<\/code> is a shallow copy of <code>list1<\/code> using the <code>copy()<\/code> method. After modifying the first element of the first inner list in <code>list1<\/code>, the same modification is reflected in <code>list2<\/code> because both lists reference the same objects.<\/p>\n\n\n\n<ol start=\"2\" class=\"has-medium-font-size wp-block-list\"><li><strong>Deep copy using <code>deepcopy()<\/code> method:<\/strong><\/li><\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>import copy\n\nlist1 = &#91;&#91;1, 2], &#91;3, 4]]\nlist2 = copy.deepcopy(list1)\n\nprint(list1)  # Output: &#91;&#91;1, 2], &#91;3, 4]]\nprint(list2)  # Output: &#91;&#91;1, 2], &#91;3, 4]]\n\nlist1&#91;0]&#91;0] = 5\n\nprint(list1)  # Output: &#91;&#91;5, 2], &#91;3, 4]]\nprint(list2)  # Output: &#91;&#91;1, 2], &#91;3, 4]]<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\">In the above example, <code>list1<\/code> is a nested list with two inner lists. <code>list2<\/code> is a deep copy of <code>list1<\/code> using the <code>deepcopy()<\/code> method. After modifying the first element of the first inner list in <code>list1<\/code>, the same modification is not reflected in <code>list2<\/code> because <code>list2<\/code> is a completely new list with new objects.<\/p>\n\n\n\n<p class=\"has-medium-font-size\">In summary, the <code>copy()<\/code> method creates a new list with references to the same objects as the original list, while the <code>deepcopy()<\/code> method creates a completely new list with new objects. Use <code>copy()<\/code> when you want to create a new list that shares the same objects as the original list, and use <code>deepcopy()<\/code> when you want to create a completely new list with new objects.<\/p>\n\n\n\n<p style=\"font-size:30px\"><strong>List comprehension<\/strong><\/p>\n\n\n\n<p class=\"has-medium-font-size\">List comprehension is a concise way to create a new list based on an existing list or iterable. It allows you to write a single line of code to create a new list by applying a function or condition to each element of an existing list. Here&#8217;s an example:<\/p>\n\n\n\n<p class=\"has-medium-font-size\">Suppose we have a list of numbers:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>numbers = &#91;1, 2, 3, 4, 5]\n<\/code><\/pre>\n\n\n\n<p>We can use list comprehension to create a new list that contains the squares of these numbers:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>squares = &#91;num**2 for num in numbers]\nprint(squares)<\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;1, 4, 9, 16, 25]<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\">In this example, we use list comprehension to create a new list <code>squares<\/code> that contains the squares of each number in the original list <code>numbers<\/code>. The expression <code>num**2<\/code> is applied to each element of the list <code>numbers<\/code>, and the resulting values are collected into a new list.<\/p>\n\n\n\n<p class=\"has-medium-font-size\">List comprehension can also include conditions. Here&#8217;s an example that uses list comprehension to create a new list that contains only the even numbers from the original list:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>even_numbers = &#91;num for num in numbers if num % 2 == 0]\nprint(even_numbers)<\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;2, 4]<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\">In this example, we add a condition <code>if num % 2 == 0<\/code> to the list comprehension, which filters out any numbers that are not even. Only the even numbers are included in the new list <code>even_numbers<\/code>.<\/p>\n\n\n\n<p class=\"has-medium-font-size\">List comprehension is a powerful feature in Python that allows you to create new lists quickly and easily, and it can help make your code more concise and readable.<\/p>\n\n\n\n<p class=\"has-medium-font-size\"><strong>Example : <\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Creating a list of squares of numbers from 1 to 10\nsquares = &#91;x ** 2 for x in range(1, 11)]\nprint(squares)\n# Output: &#91;1, 4, 9, 16, 25, 36, 49, 64, 81, 100]\n\n# Creating a list of even numbers from a given list:\nnumbers = &#91;1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\neven_numbers = &#91;x for x in numbers if x % 2 == 0]\nprint(even_numbers)\n# Output: &#91;1, 4, 9, 16, 25, 36, 49, 64, 81, 100]\n\n#Creating a list of even numbers from a given list:\nnumbers = &#91;1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\neven_numbers = &#91;x for x in numbers if x % 2 == 0]\nprint(even_numbers)\n# Output: &#91;2, 4, 6, 8, 10]\n\n#Creating a list of common elements from two given lists\nlist1 = &#91;1, 2, 3, 4, 5]\nlist2 = &#91;3, 4, 5, 6, 7]\ncommon_elements = &#91;x for x in list1 if x in list2]\nprint(common_elements)\n# Output: &#91;3, 4, 5]\n\n#Creating a list of strings from a list of integers:\nnumbers = &#91;1, 2, 3, 4, 5]\nstrings = &#91;str(x) for x in numbers]\nprint(strings)\n#Output: &#91;'1', '2', '3', '4', '5']\n\n# Creating a list of tuples from two lists:\nlist1 = &#91;'apple', 'banana', 'cherry']\nlist2 = &#91;1, 2, 3]\nresult = &#91;(x, y) for x in list1 for y in list2]\nprint(result)\n# Output: &#91;('apple', 1), ('apple', 2), ('apple', 3), ('banana', 1), ('banana', 2), \n# ('banana', 3), ('cherry', 1), ('cherry', 2), ('cherry', 3)<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p class=\"has-background\" style=\"background-color:#e0e6e8;font-size:24px\"><strong>Questions for Practice <\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>What is list ? Explain the concepts of list creation and list slicing with examples.<\/li><li>Define Tuple, List, Strings and Dictionary in Python.<\/li><li>Explain why lists are called Mutable.<\/li><li>Discuss the following List operations and functions with examples :<ul><li>Accessing ,Traversing and Slicing the List Elements<\/li><li>+ (concatenation) and * (Repetition)<\/li><li>append, extend, sort, remove and delete<\/li><li>split and join<\/li><\/ul><\/li><li>Explain the methods of list data type in Python for the following operations with suitable code snippets for each.<ul><li>a. Adding values to a list<\/li><li>b. Removing values from a list<\/li><li>c. Finding a value in a list<\/li><li>d. Sorting the values in a list<\/li><\/ul><\/li><li>What is list ? Explain the concept of slicing and indexing with proper examples.<\/li><li>For a given list num = [45,22,14,65,97,72] write a python program to replace all the integers divisible by 3 with\u201dppp\u201d and all integers divisible by 5 with \u201cqqq\u201d and replace all the integers divisible by both 3 and 5 with\u201d pppqqq\u201d and display the output.<\/li><li>What are the different LIST methods supported in python . Illustrate all the methods with an example.<\/li><li>What is a Python list?<\/li><li>How is a Python list different from other data types in Python?<\/li><li>Can a Python list contain elements of different data types?<\/li><li>What is the difference between indexing and slicing in a Python list?<\/li><li>How does Python handle out of index errors when working with lists?<\/li><li>How do you create an empty Python list?<\/li><li>How do you check if a Python list is empty?<\/li><li>What is the difference between append() and extend() methods for adding elements to a Python list?<\/li><li>How does the remove() method work in a Python list?<\/li><li>How do you use the pop() method in a Python list?<\/li><li>How do you find the index of an element in a Python list?<\/li><li>How do you count the occurrences of an element in a Python list?<\/li><li>What is a nested Python list?<\/li><li>How do you flatten a nested Python list?<\/li><li>How do you loop through a nested Python list?<\/li><li>What is a list comprehension in Python?<\/li><li>How do you filter elements in a Python list using list comprehension?<\/li><li>How do you use map() and filter() functions with a Python list?<\/li><li>How do you sort a Python list in descending order?<\/li><li>How do you remove duplicates from a Python list?<\/li><li>Write a Python program to create a list of numbers and print the sum of all the elements in the list.<\/li><li>Write a Python program to create a list of strings and print the longest string in the list.<\/li><li>Write a Python program to create a list of numbers and remove all the even numbers from the list.<\/li><li>Write a Python program to create a list of numbers and print the second highest number in the list.<\/li><li>Write a Python program to create a list of strings and sort the list in alphabetical order.<\/li><li>Write a Python program to create a list of numbers and find the average of all the elements in the list.<\/li><li>Write a Python program to create a list of numbers and print the median of the list.<\/li><li>Write a Python program to create a list of strings and count the number of words in the list that have more than 5 characters.<\/li><li>Write a Python program to create a list of numbers and find the difference between the largest and smallest elements in the list.<\/li><li>Write a Python program to create a list of numbers and print the elements that are divisible by 3.<\/li><li>Write a Python program to create a list of numbers and remove all the elements that are less than 5.<\/li><li>Write a Python program to create a list of numbers and print the elements that occur more than once in the list.<\/li><li>Write a Python program to create a list of numbers and sort the list in descending order.<\/li><li>Write a Python program to create a list of strings and print all the strings that start with the letter &#8220;A&#8221;.<\/li><li>Write a Python program to create a list of numbers and find the second smallest number in the list.<\/li><li>Write a Python program to create a nested list and print all the elements in the list.<\/li><li>Write a Python program to create a list of strings and use list comprehension to create a new list with only the strings that contain the letter &#8220;a&#8221;.<\/li><li>Write a Python program to create a list of numbers and use map() function to add 5 to each element in the list.<\/li><li>Write a Python program to create a list of numbers and use filter() function to remove all the odd numbers from the list.<\/li><li>Write a Python program to create a list of numbers and remove all the duplicate elements from the list.<\/li><li>Write a Python program to create a list of numbers and find the sum of all the elements at even indices.<\/li><li>Write a Python program to create a list of strings and remove all the strings that contain the letter &#8220;e&#8221;.<\/li><li>Write a Python program to create a list of numbers and find the product of all the elements in the list.<\/li><li>Write a Python program to create a list of strings and print the strings that are palindromes.<\/li><li>Write a Python program to create a list of numbers and remove all the elements that are not prime numbers.<\/li><li>Write a Python program to create a list of numbers and find the largest prime number in the list.<\/li><li>Write a Python program to create a list of strings and print the strings that are anagrams of each other.<\/li><li>Write a Python program to create a list of numbers and find the kth smallest number in the list.<\/li><li>Write a Python program to create a list of strings and print the strings that are in title case.<\/li><li>Write a Python program to create a list of numbers and find the sum of all the elements that are divisible by 3 or 5.<\/li><li>Write a Python program to create a list of strings and remove all the strings that have more than 3 vowels.<\/li><li>Write a Python program to create a list of numbers and print the elements that are greater than the average of all the elements in the list.<\/li><li>Write a Python program to create a list of strings and print the strings that contain both vowels and consonants.<\/li><li>Write a Python program to create a list of numbers and find the element that appears the most number of times in the list.<\/li><li>Write a Python program to create a list of strings and print the strings that are in uppercase.<\/li><li>Write a Python program to create a list of numbers and find the difference between the sum of even numbers and the sum of odd numbers in the list.<\/li><li>Write a Python program to create a list of strings and remove all the strings that have a length less than 3.<\/li><li>Write a Python program to create a list of numbers and find the second highest even number in the list.<\/li><li>Write a Python program to create a list of strings and print the strings that have a length that is a multiple of 3.<\/li><li>Write a Python program to create a list of numbers and find the number of times the sum of adjacent elements is even in the list.<\/li><\/ol>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Syllabus : Lists:&nbsp;The List Data Type, Working with Lists, Augmented Assignment Operators, Methods,&nbsp;Example Program:&nbsp;Magic 8 Ball with a List, List-like Types: Strings and Tuples, References, In Python, a sequence is&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":"","_links_to":"","_links_to_target":""},"class_list":["post-25310","page","type-page","status-publish","hentry"],"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/tocxten.com\/index.php\/wp-json\/wp\/v2\/pages\/25310","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tocxten.com\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/tocxten.com\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/tocxten.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tocxten.com\/index.php\/wp-json\/wp\/v2\/comments?post=25310"}],"version-history":[{"count":107,"href":"https:\/\/tocxten.com\/index.php\/wp-json\/wp\/v2\/pages\/25310\/revisions"}],"predecessor-version":[{"id":25539,"href":"https:\/\/tocxten.com\/index.php\/wp-json\/wp\/v2\/pages\/25310\/revisions\/25539"}],"wp:attachment":[{"href":"https:\/\/tocxten.com\/index.php\/wp-json\/wp\/v2\/media?parent=25310"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}