{"id":433,"date":"2024-07-18T11:43:31","date_gmt":"2024-07-18T06:43:31","guid":{"rendered":"https:\/\/augurytech.co.uk\/courses\/?p=433"},"modified":"2024-10-05T09:05:53","modified_gmt":"2024-10-05T04:05:53","slug":"python-lists","status":"publish","type":"post","link":"https:\/\/augurytech.co.uk\/courses\/python\/python-lists\/","title":{"rendered":"Python: Lists"},"content":{"rendered":"\n<ul class=\"wp-block-list\">\n<li>A list is a <strong>data structure<\/strong> in Python that is a<strong> mutable<\/strong>, or changeable, <strong>ordered sequence of elements<\/strong>.<\/li>\n\n\n\n<li>Each element or value that is inside of a list is called an item.<\/li>\n\n\n\n<li>Just as strings are defined as characters between quotes, lists are defined by having values between square brackets <strong>[ ]<\/strong> and every value is separated by comma &#8221; <strong>,<\/strong> &#8220;.<\/li>\n<\/ul>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<div class=\"wp-block-group is-vertical is-content-justification-stretch is-layout-flex wp-container-core-group-is-layout-353c4f5a wp-block-group-is-layout-flex\">\n<h3 class=\"wp-block-heading\">Syntax<\/h3>\n\n\n\n<pre class=\"wp-block-code\" style=\"padding-top:var(--wp--preset--spacing--30);padding-bottom:var(--wp--preset--spacing--30);font-size:clamp(1.125rem, 1.125rem + ((1vw - 0.2rem) * 0.204), 1.25rem);\"><code><strong>friends = &#91;'Hamza', 'Fatima', 'Aaliyan', 'Anzla', 'Ali', 'Zoya']<\/strong><\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\" style=\"padding-top:var(--wp--preset--spacing--30);padding-bottom:var(--wp--preset--spacing--30);font-size:clamp(1.125rem, 1.125rem + ((1vw - 0.2rem) * 0.204), 1.25rem);\"><code><strong>numbers = &#91;1,2,3,4,5,6,7,8,9]<\/strong><\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\" style=\"padding-top:var(--wp--preset--spacing--30);padding-bottom:var(--wp--preset--spacing--30);font-size:clamp(1.125rem, 1.125rem + ((1vw - 0.2rem) * 0.204), 1.25rem);\"><code><strong>list_float = &#91; 2.43,98.67, 7.45, 45.65]<\/strong><\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\" style=\"padding-top:var(--wp--preset--spacing--30);padding-bottom:var(--wp--preset--spacing--30);font-size:clamp(1.125rem, 1.125rem + ((1vw - 0.2rem) * 0.204), 1.25rem);\"><code><strong>mix = &#91;'Ahsan', 2, 4.56, 6.87, True, 'Maham', 56, false]<\/strong><\/code><\/pre>\n<\/div>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<div class=\"wp-block-group is-vertical is-layout-flex wp-container-core-group-is-layout-fe9cc265 wp-block-group-is-layout-flex\">\n<h3 class=\"wp-block-heading\">Common List Operations<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Access value<\/li>\n\n\n\n<li>Add a new value at the end\/tail of the list<\/li>\n\n\n\n<li>Find the index of a value in the list<\/li>\n\n\n\n<li>Slicing the elements from the list<\/li>\n\n\n\n<li>Deleting and removing the elements from the List<\/li>\n\n\n\n<li>Popping elements from the list<\/li>\n<\/ul>\n<\/div>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<div class=\"wp-block-group is-vertical is-content-justification-stretch is-layout-flex wp-container-core-group-is-layout-353c4f5a wp-block-group-is-layout-flex\">\n<h3 class=\"wp-block-heading\">Accessing List Elements<\/h3>\n\n\n\n<p>List elements are accessed by providing the identifier and index of elements inside square brackets. For example, in the following snippet <strong>arr<\/strong> is an identifier which points to a list.<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"padding-top:var(--wp--preset--spacing--30);padding-bottom:var(--wp--preset--spacing--30);font-size:clamp(1.125rem, 1.125rem + ((1vw - 0.2rem) * 0.204), 1.25rem);\"><code><strong>arr = &#91;1, True, \"Pakistan\", 3.5, 5, 9]<\/strong><\/code><\/pre>\n\n\n\n<p>To access the element at Position 3 which is &#8220;<strong>Pakistan<\/strong>&#8220;, we write:<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"padding-top:var(--wp--preset--spacing--30);padding-bottom:var(--wp--preset--spacing--30);font-size:clamp(1.125rem, 1.125rem + ((1vw - 0.2rem) * 0.204), 1.25rem);\"><code><strong>print (arr&#91;2])<\/strong><\/code><\/pre>\n\n\n\n<p>Please note that to access position 3, we have provided index 2, because the first element in the Python List always starts with a <strong>0 (Zero) index<\/strong>.<\/p>\n<\/div>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<div class=\"wp-block-group is-vertical is-content-justification-stretch is-layout-flex wp-container-core-group-is-layout-353c4f5a wp-block-group-is-layout-flex\">\n<h3 class=\"wp-block-heading\">Adding Values in the List<\/h3>\n\n\n\n<p>Python provides a function <strong>append()<\/strong> which <strong>adds the value at the end of the List<\/strong>. For example, arr points to a list in the following snippet<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"padding-top:var(--wp--preset--spacing--30);padding-bottom:var(--wp--preset--spacing--30);font-size:clamp(1.125rem, 1.125rem + ((1vw - 0.2rem) * 0.204), 1.25rem);\"><code><strong>arr = &#91;1, -3, 4.5, 32, 0.2, 4, 6]<\/strong><\/code><\/pre>\n\n\n\n<p>To <strong>add a value 321<\/strong> to the list we can do the following<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"padding-top:var(--wp--preset--spacing--30);padding-bottom:var(--wp--preset--spacing--30);font-size:clamp(1.125rem, 1.125rem + ((1vw - 0.2rem) * 0.204), 1.25rem);\"><code><strong>arr.append(321)<\/strong><\/code><\/pre>\n\n\n\n<p>Now if we check the elements in the list <strong><code>arr<\/code><\/strong> we will get the following content<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"padding-top:var(--wp--preset--spacing--30);padding-bottom:var(--wp--preset--spacing--30);font-size:clamp(1.125rem, 1.125rem + ((1vw - 0.2rem) * 0.204), 1.25rem);\"><code><strong>arr = &#91;1, -3, 4.5, 32, 0.2, 4, 6, 321]<\/strong><\/code><\/pre>\n<\/div>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<div class=\"wp-block-group is-vertical is-content-justification-stretch is-layout-flex wp-container-core-group-is-layout-353c4f5a wp-block-group-is-layout-flex\">\n<h3 class=\"wp-block-heading\">Searching\/Finding a Value in List<\/h3>\n\n\n\n<p>Python provides a function <strong>index ()<\/strong> which returns the index of the first occurrence of the value. For example, if <strong>arr<\/strong> points to a List<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"padding-top:var(--wp--preset--spacing--30);padding-bottom:var(--wp--preset--spacing--30);font-size:clamp(1.125rem, 1.125rem + ((1vw - 0.2rem) * 0.204), 1.25rem);\"><code><strong>arr = &#91;1, -3, 4.5, 32, 0.2, 4, 1]<\/strong><\/code><\/pre>\n\n\n\n<p>To find a value <strong>1<\/strong> in the list we can do the following<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"padding-top:var(--wp--preset--spacing--30);padding-bottom:var(--wp--preset--spacing--30);font-size:clamp(1.125rem, 1.125rem + ((1vw - 0.2rem) * 0.204), 1.25rem);\"><code><strong>arr.index(1)<\/strong><\/code><\/pre>\n\n\n\n<p>This call will give the <strong>result 0<\/strong> even if you execute this line many times, the reason is by default this function <strong>starts searching from index 0<\/strong> and stops when it finds the first occurrence of the desired value.<\/p>\n<\/div>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<div class=\"wp-block-group is-vertical is-content-justification-stretch is-layout-flex wp-container-core-group-is-layout-353c4f5a wp-block-group-is-layout-flex\">\n<h3 class=\"wp-block-heading\">Slicing the elements from the List<\/h3>\n\n\n\n<p>Python provides a very handy feature if you want to take a partial piece of a List. Note that the slicing operation copies elements from the original List. Hence this operation returns a new List which is a subset of the original List. Example syntax:<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"padding-top:var(--wp--preset--spacing--30);padding-bottom:var(--wp--preset--spacing--30);font-size:clamp(1.125rem, 1.125rem + ((1vw - 0.2rem) * 0.204), 1.25rem);\"><code><strong>arr = &#91;1, 4, 2, 78, 45, 23, 89]<\/strong>\n\n<strong>print(arr&#91;1: 4]) <\/strong><\/code><\/pre>\n\n\n\n<p class=\"has-large-font-size\"><strong>Outputs =&gt; [4, 2, 78]<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"padding-top:var(--wp--preset--spacing--30);padding-bottom:var(--wp--preset--spacing--30);font-size:clamp(1.125rem, 1.125rem + ((1vw - 0.2rem) * 0.204), 1.25rem);\"><code><strong>print(arr&#91;-2: -1])<\/strong><\/code><\/pre>\n\n\n\n<p class=\"has-large-font-size\"><strong>Outputs =&gt; [23]<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"padding-top:var(--wp--preset--spacing--30);padding-bottom:var(--wp--preset--spacing--30);font-size:clamp(1.125rem, 1.125rem + ((1vw - 0.2rem) * 0.204), 1.25rem);\"><code><strong>print(arr&#91;1: : 2])<\/strong><\/code><\/pre>\n\n\n\n<p class=\"has-large-font-size\"><strong>Outputs =&gt; [4, 78, 23]<\/strong><\/p>\n<\/div>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<div class=\"wp-block-group is-vertical is-content-justification-stretch is-layout-flex wp-container-core-group-is-layout-353c4f5a wp-block-group-is-layout-flex\">\n<h3 class=\"wp-block-heading\">Deleting and removing the value from the List<\/h3>\n\n\n\n<p>Although both are synonyms, Python and how they are used differ.<\/p>\n\n\n\n<div class=\"wp-block-group is-vertical is-content-justification-stretch is-layout-flex wp-container-core-group-is-layout-353c4f5a wp-block-group-is-layout-flex\">\n<ol class=\"wp-block-list\">\n<li>There are cases when we know exactly the index of value we need to delete, in such cases we use the <strong>del<\/strong> keyword to delete the element. Example<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\" style=\"padding-top:var(--wp--preset--spacing--30);padding-bottom:var(--wp--preset--spacing--30);font-size:clamp(1.125rem, 1.125rem + ((1vw - 0.2rem) * 0.204), 1.25rem);\"><code><strong>arr = &#91;1, 3, 2, 6, 4]<\/strong>\n\n<strong>del arr&#91;2]<\/strong><\/code><\/pre>\n\n\n\n<p>Now if we check the contents of List <strong>arr<\/strong> we will get the following output<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"padding-top:var(--wp--preset--spacing--30);padding-bottom:var(--wp--preset--spacing--30);font-size:clamp(1.125rem, 1.125rem + ((1vw - 0.2rem) * 0.204), 1.25rem);\"><code><strong>arr = &#91;1, 3, 6, 4]<\/strong><\/code><\/pre>\n<\/div>\n\n\n\n<div style=\"height:0px\" aria-hidden=\"true\" class=\"wp-block-spacer wp-container-content-c00f5982\"><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-wide\"\/>\n\n\n\n<div class=\"wp-block-group is-vertical is-content-justification-stretch is-layout-flex wp-container-core-group-is-layout-353c4f5a wp-block-group-is-layout-flex\">\n<p>2. In case we know the value but do not know the index of that value in List, we call the remove function over the List identifier and pass the value to the function. Example<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"padding-top:var(--wp--preset--spacing--30);padding-bottom:var(--wp--preset--spacing--30);font-size:clamp(1.125rem, 1.125rem + ((1vw - 0.2rem) * 0.204), 1.25rem);\"><code><strong>arr = &#91;1, 3, 2, 6, 4]<\/strong>\n<strong>arr.remove(2)<\/strong><\/code><\/pre>\n\n\n\n<p>Now if we check the contents of List<strong> arr<\/strong> we will get the following output<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"padding-top:var(--wp--preset--spacing--30);padding-bottom:var(--wp--preset--spacing--30);font-size:clamp(1.125rem, 1.125rem + ((1vw - 0.2rem) * 0.204), 1.25rem);\"><code><strong>arr = &#91;1, 3, 6, 4]<\/strong><\/code><\/pre>\n<\/div>\n<\/div>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<div class=\"wp-block-group is-vertical is-content-justification-stretch is-layout-flex wp-container-core-group-is-layout-353c4f5a wp-block-group-is-layout-flex\">\n<h3 class=\"wp-block-heading\">Popping Elements from the List<\/h3>\n\n\n\n<p>Popping elements works in two ways<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>We know the index of the value to remove<\/li>\n\n\n\n<li>We want to remove the element at the end \/ tail of List<\/li>\n<\/ol>\n\n\n\n<p>Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"padding-top:var(--wp--preset--spacing--30);padding-bottom:var(--wp--preset--spacing--30);font-size:clamp(1.125rem, 1.125rem + ((1vw - 0.2rem) * 0.204), 1.25rem);\"><code><strong>arr = &#91;1, 2, 4, 6, 7]\narr.pop() # will remove the value 7 from the end of List\narr.pop(2) # will remove the value 4 from the index 2 of List<\/strong><\/code><\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Syntax Common List Operations Accessing List Elements List elements are accessed by providing the identifier and index of elements inside square brackets. For example, in the following snippet arr is an identifier which points to a list. To access the element at Position 3 which is &#8220;Pakistan&#8220;, we write: Please note that to access position [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"wp-custom-template-python","format":"standard","meta":{"_seopress_robots_primary_cat":"","_seopress_titles_title":"","_seopress_titles_desc":"","_seopress_robots_index":"","footnotes":""},"categories":[5],"tags":[],"class_list":{"0":"post-433","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-python"},"_links":{"self":[{"href":"https:\/\/augurytech.co.uk\/courses\/wp-json\/wp\/v2\/posts\/433","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/augurytech.co.uk\/courses\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/augurytech.co.uk\/courses\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/augurytech.co.uk\/courses\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/augurytech.co.uk\/courses\/wp-json\/wp\/v2\/comments?post=433"}],"version-history":[{"count":30,"href":"https:\/\/augurytech.co.uk\/courses\/wp-json\/wp\/v2\/posts\/433\/revisions"}],"predecessor-version":[{"id":566,"href":"https:\/\/augurytech.co.uk\/courses\/wp-json\/wp\/v2\/posts\/433\/revisions\/566"}],"wp:attachment":[{"href":"https:\/\/augurytech.co.uk\/courses\/wp-json\/wp\/v2\/media?parent=433"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/augurytech.co.uk\/courses\/wp-json\/wp\/v2\/categories?post=433"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/augurytech.co.uk\/courses\/wp-json\/wp\/v2\/tags?post=433"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}