{"id":236,"date":"2023-03-29T04:44:15","date_gmt":"2023-03-29T04:44:15","guid":{"rendered":"https:\/\/joseph-t-gordon.tech\/?p=236"},"modified":"2023-06-26T14:24:00","modified_gmt":"2023-06-26T14:24:00","slug":"post-40-more-options-with-lists-and-the-use-of-in-and-not-in","status":"publish","type":"post","link":"https:\/\/joseph-t-gordon.tech\/index.php\/2023\/03\/29\/post-40-more-options-with-lists-and-the-use-of-in-and-not-in\/","title":{"rendered":"Post #40 (Python Programming) &#8211; More options with lists and the use of in and not in."},"content":{"rendered":"\n<p>Let start off by taking a look at the following code.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>list = [1, 2, 3, 4, 5]<\/p>\n\n\n\n<p>list_2= list<\/p>\n\n\n\n<p>del list[1]<\/p>\n\n\n\n<p>print(list_2)<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Now the following code should return the results [1, 2, 3, 4, 5] right? Wrong, the code returns [1, 3, 4, 5]<\/p>\n\n\n\n<p>This is because you are setting the list equal to the current location of the other list, not the list&#8217;s content. When the code looks at the location of the data for list_2 it looks in list since that is what list_2 is set to. In order to copy only the elements within a list the following code can be used list_var = list[start:stop]. The first element listed is where the list will begin, the second is the first element the list will not include. If both are left blank then this will indicate the entirety of the list&#8217;s contents will be copied.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Code:<\/p>\n\n\n\n<p>list = [1, 2, 3, 4, 5]<\/p>\n\n\n\n<p>list_2= list[:]<\/p>\n\n\n\n<p>del list[1]<\/p>\n\n\n\n<p>print(list_2)<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Result:<\/p>\n\n\n\n<p>[1, 2, 3, 4, 5]<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Here&#8217;s another example with different values<\/p>\n\n\n\n<p>list = [1, 2, 3, 4, 5]<\/p>\n\n\n\n<p>list_2= list[2:4]<\/p>\n\n\n\n<p>del list[3]<\/p>\n\n\n\n<p>print(list)<\/p>\n\n\n\n<p>print(list_2)<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Result:<\/p>\n\n\n\n<p>[1, 2, 3, 5]<\/p>\n\n\n\n<p>[3, 4]<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Now for the use of the in and not in operators with lists, it is rather simple, <span style=\"text-decoration: underline;\">element in list_name<\/span> and <span style=\"text-decoration: underline;\">element not in list_name<\/span> how it works is the code will check to see if the element is or isn&#8217;t in the list and return the appropriate result, true or false.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Code:<\/p>\n\n\n\n<p>list = [1, 2, 3, 4, 5]<\/p>\n\n\n\n<p>print(1 in list)<\/p>\n\n\n\n<p>print(7 not in list)<\/p>\n\n\n\n<p>print(6 in list)<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Result:<\/p>\n\n\n\n<p>True<\/p>\n\n\n\n<p>True<\/p>\n\n\n\n<p>False<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Here is a more interesting use of the in and not in operator, the following code will go through each item in the list and add unique numbers to a new list and print the result. I&#8217;ve also added the sort method as well to show how multiple operators and methods can be used to sort the list into unique elements and sort them in ascending order!<\/p>\n\n\n\n<p>Code:<\/p>\n\n\n\n<p>numbers = [1, 2, 2, 2, 4, 3, 3, 6, 4, 5, 9, 6, 6, 7, 7, 8, 9, 10]<\/p>\n\n\n\n<p>numbers_new = []<\/p>\n\n\n\n<p>for i in numbers:<\/p>\n\n\n\n<p>    if i not in numbers_new:<\/p>\n\n\n\n<p>        numbers_new.append(i)<\/p>\n\n\n\n<p>print(numbers_new)<\/p>\n\n\n\n<p>numbers_new.sort()<\/p>\n\n\n\n<p>print(numbers_new)<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Result:<\/p>\n\n\n\n<p>[1, 2, 4, 3, 6, 5, 9, 7, 8, 10]<\/p>\n\n\n\n<p>[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Let start off by taking a look at the following code. list = [1, 2, 3, 4, 5] list_2= list del list[1] print(list_2) Now the following code should return the results [1, 2, 3, 4, 5] right? Wrong, the code returns [1, 3, 4, 5] This is because you are setting the list equal to&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/joseph-t-gordon.tech\/index.php\/wp-json\/wp\/v2\/posts\/236"}],"collection":[{"href":"https:\/\/joseph-t-gordon.tech\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/joseph-t-gordon.tech\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/joseph-t-gordon.tech\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/joseph-t-gordon.tech\/index.php\/wp-json\/wp\/v2\/comments?post=236"}],"version-history":[{"count":2,"href":"https:\/\/joseph-t-gordon.tech\/index.php\/wp-json\/wp\/v2\/posts\/236\/revisions"}],"predecessor-version":[{"id":324,"href":"https:\/\/joseph-t-gordon.tech\/index.php\/wp-json\/wp\/v2\/posts\/236\/revisions\/324"}],"wp:attachment":[{"href":"https:\/\/joseph-t-gordon.tech\/index.php\/wp-json\/wp\/v2\/media?parent=236"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/joseph-t-gordon.tech\/index.php\/wp-json\/wp\/v2\/categories?post=236"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/joseph-t-gordon.tech\/index.php\/wp-json\/wp\/v2\/tags?post=236"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}