{"id":13,"date":"2016-01-06T15:54:43","date_gmt":"2016-01-06T15:54:43","guid":{"rendered":"http:\/\/mr-west.uk\/python\/?page_id=13"},"modified":"2017-01-22T10:19:26","modified_gmt":"2017-01-22T10:19:26","slug":"lists","status":"publish","type":"page","link":"https:\/\/learnlearn.uk\/python\/lists\/","title":{"rendered":"Lists"},"content":{"rendered":"\n<div class=\"arconix-tabs-horizontal\"><ul class=\"arconix-tabs\"><li data-arconix-icon=\" \" data-arconix-color=\" \" class=\"arconix-tab tab-TutorialVideo\"><a class=\"\" href=\"#tab-TutorialVideo\">Tutorial Video<\/a><\/li>\n<li data-arconix-icon=\" \" data-arconix-color=\" \" class=\"arconix-tab tab-Creatinglists\"><a class=\"\" href=\"#tab-Creatinglists\">Creating lists<\/a><\/li>\n<li data-arconix-icon=\" \" data-arconix-color=\" \" class=\"arconix-tab tab-Addingitems\"><a class=\"\" href=\"#tab-Addingitems\">Adding items<\/a><\/li>\n<li data-arconix-icon=\" \" data-arconix-color=\" \" class=\"arconix-tab tab-Removingitems\"><a class=\"\" href=\"#tab-Removingitems\">Removing items<\/a><\/li>\n<li data-arconix-icon=\" \" data-arconix-color=\" \" class=\"arconix-tab tab-Looping\"><a class=\"\" href=\"#tab-Looping\">Looping<\/a><\/li>\n<li data-arconix-icon=\" \" data-arconix-color=\" \" class=\"arconix-tab tab-2DLists\"><a class=\"\" href=\"#tab-2DLists\">2D Lists<\/a><\/li>\n<li data-arconix-icon=\" \" data-arconix-color=\" \" class=\"arconix-tab tab-Methods\"><a class=\"\" href=\"#tab-Methods\">Methods<\/a><\/li>\n<li data-arconix-icon=\" \" data-arconix-color=\" \" class=\"arconix-tab tab-Practice\"><a class=\"\" href=\"#tab-Practice\">Practice<\/a><\/li>\n<li data-arconix-icon=\" \" data-arconix-color=\" \" class=\"arconix-tab tab-Resources\"><a class=\"\" href=\"#tab-Resources\">Resources<\/a><\/li><\/ul>\n<div class=\"arconix-panes\"><div class=\"arconix-pane pane-TutorialVideo\">\n<p><a href=\"https:\/\/www.youtube.com\/playlist?list=PLfd-8l91Lb4VclnjJMvc3f4qVvmOp0bZb\">Video playlist also available on YouTube.<\/a><br \/>\n<iframe loading=\"lazy\" src=\"https:\/\/drive.google.com\/file\/d\/0B4M7J3ubGpimbjhaa1V2RUhLRzA\/preview\" width=\"800\" height=\"600\"><\/iframe><\/p>\n<\/div>\n<div class=\"arconix-pane pane-Creatinglists\">\n<h3>Creating an empty list in Python \u00a0<a href=\"https:\/\/www.youtube.com\/watch?v=V0tSGodNTYw\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-835 size-full\" src=\"https:\/\/learnlearn.uk\/python\/wp-content\/uploads\/sites\/4\/2016\/01\/yttinyiconblue.png?_t=1485080279\" alt=\"yttinyiconblue\" width=\"14\" height=\"14\" \/><\/a><\/h3>\n<p>Creating an empty list is easy, you just use the square backets [ ].<\/p>\n<pre lang=\"python\">myList = []<\/pre>\n<h3>Creating a list with items in it.<\/h3>\n<pre lang=\"python\">animals = [\"dog\",\"cat\",\"mouse\"]<\/pre>\n<\/div>\n<div class=\"arconix-pane pane-Addingitems\">\n<h3>Adding items to a list \u00a0<a href=\"https:\/\/www.youtube.com\/watch?v=Ys-oxElF1h8\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-835 size-full\" src=\"https:\/\/learnlearn.uk\/python\/wp-content\/uploads\/sites\/4\/2016\/01\/yttinyiconblue.png?_t=1485080279\" alt=\"yttinyiconblue\" width=\"14\" height=\"14\" \/><\/a><\/h3>\n<p><strong>Adding to the end of the list<\/strong><\/p>\n<p>If you want to add an item to the end of a list, you need to use the append method.<\/p>\n<pre lang=\"python\">animals = [\"dog\",\"cat\",\"mouse\"]\r\nanimals.append(\"chicken\")<\/pre>\n<p><strong>Adding to the beginning of the list<\/strong><\/p>\n<p>If you want add an item to beginning of a list(or at any given position), you need to use the insert method.<\/p>\n<pre lang=\"python\">animals.insert(0,\"bat\")<\/pre>\n<\/div>\n<div class=\"arconix-pane pane-Removingitems\">\n<h3>Removing items from lists \u00a0<a href=\"https:\/\/www.youtube.com\/watch?v=l-nZ9wjREoA\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-835 size-full\" src=\"https:\/\/learnlearn.uk\/python\/wp-content\/uploads\/sites\/4\/2016\/01\/yttinyiconblue.png?_t=1485080279\" alt=\"yttinyiconblue\" width=\"14\" height=\"14\" \/><\/a><\/h3>\n<p>When removing items from a list you can use a number of different methods.<\/p>\n<pre lang=\"python\">myList = [\"Leonardo\",\"Raphael\",\"Donatello\",\"Michaelangelo\"]<\/pre>\n<h3>The list.pop() method<\/h3>\n<p>This returns the first item from the list and deletes the item from the list. Useful if you have a list of items that you want to process and want to discard each item once you have finished with it.<\/p>\n<pre lang=\"python\">item = mylist.pop()\r\n\r\nprint (\"Goodbye\", item)<\/pre>\n<p>&nbsp;<\/p>\n<h3>the del list[i] function<\/h3>\n<p>This is similar to the pop() method but it just deletes the item.<\/p>\n<pre lang=\"python\">del myList[0] #Delete the first item from the list<\/pre>\n<\/div>\n<div class=\"arconix-pane pane-Looping\">\n<h3>Looping through lists<strong> \u00a0<a href=\"https:\/\/www.youtube.com\/watch?v=N4qcaYNNrTA\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-835 size-full\" src=\"https:\/\/learnlearn.uk\/python\/wp-content\/uploads\/sites\/4\/2016\/01\/yttinyiconblue.png?_t=1485080279\" alt=\"yttinyiconblue\" width=\"14\" height=\"14\" \/><\/a><\/strong><\/h3>\n<p>Looping through a list in Python is easy, we just use a for loop!<\/p>\n<pre lang=\"python\">mixedList = [\"dog\", 4, 3.42,\"cat\"]\r\nfor item in mixedList:\r\n    print(item)<\/pre>\n<h3>Using if statements in lists \u00a0<a href=\"https:\/\/www.youtube.com\/watch?v=5mjggVFDc3w\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-835 size-full\" src=\"https:\/\/learnlearn.uk\/python\/wp-content\/uploads\/sites\/4\/2016\/01\/yttinyiconblue.png?_t=1485080279\" alt=\"yttinyiconblue\" width=\"14\" height=\"14\" \/><\/a><\/h3>\n<p>Of course you can also use if statements while looping through your lists.<\/p>\n<pre lang=\"python\">for temp in [-50,-30,10,40,50,100]:\r\n    if temp &gt; 80:\r\n        print(temp, \" Degrees. Wow that's hot!\")\r\n    elif temp &gt; 20:\r\n        print (temp, \" Degrees. Mmmm nice and warm :-) \")\r\n    else:\r\n        print(temp, \" Degrees. Freezing!!!!\")<\/pre>\n<\/div>\n<div class=\"arconix-pane pane-2DLists\">\n<h3>Create 2 Dimensional lists \u00a0<a href=\"https:\/\/www.youtube.com\/watch?v=omZrvPxupvQ\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-835 size-full\" src=\"https:\/\/learnlearn.uk\/python\/wp-content\/uploads\/sites\/4\/2016\/01\/yttinyiconblue.png?_t=1485080279\" alt=\"yttinyiconblue\" width=\"14\" height=\"14\" \/><\/a><\/h3>\n<p>Sometimes we want to represent table data in python, and to do this we need to use a 2D list.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-402\" src=\"https:\/\/learnlearn.uk\/python\/wp-content\/uploads\/sites\/4\/2016\/01\/namesagestable.png\" alt=\"namesagestable\" width=\"137\" height=\"110\" \/><\/p>\n<p>Sometimes \u00a0we want to represent tabled data in lists within Python. We can achieve this through the use of 2 dimensional lists(arrays) &#8211; lists within lists.<\/p>\n<pre lang=\"python\">nameList = [[\"bob\",20],[\"Sally\",45],[\"Julie\",30],[\"Fred\",15]]\r\nfor name,age in nameList:\r\n\u00a0 \u00a0 print(name ,\"is \", age, \"years old.\")\r\n\r\n<\/pre>\n<h3>Zipping lists together and unzipping them<\/h3>\n<p>If you want to store the names and ages in two separate lists, then you can use the zip function to combine them!<\/p>\n<pre lang=\"python\">names = [\"bob\",\"sally\",\"fred\",\"harry\"]\r\nages = [20,40,35,200]\r\nfor name,age in zip(names,ages):\r\n    print(name, age)<\/pre>\n<p>This method of combining separate lists is also much more readable!<\/p>\n<\/div>\n<div class=\"arconix-pane pane-Methods\">\n<p>Lists have lots of inbuilt methods that are very useful!<\/p>\n<pre lang=\"python\">myList = [\"Treacle\",\"Custard\",\"Chicken\"]\r\n\r\nmyList.sort() # Sorts the list in place\r\n\r\nmyList.reverse() # Reverses the list\r\n\r\nmyList.pop() #Returns the first item from the list and removes it from the list\r\n\r\nmyList.count(\"Chicken\") # Returns the number of times an item can be found in the list\r\n\r\n\"Dog\" in myList # Can be used with if statements to see if an item is in a list\r\n\r\nmyList.index(\"Treacle\") # Returns the position of the item in the list.<\/pre>\n<p>&nbsp;<\/p>\n<\/div>\n<div class=\"arconix-pane pane-Practice\">\nhref=&#8221;http:\/\/openbookproject.net\/thinkcs\/python\/english3e\/lists.html&#8221;&gt;http:\/\/openbookproject.net\/thinkcs\/python\/english3e\/lists.html<\/p>\n<\/div>\n<div class=\"arconix-pane pane-Resources\">\n<p><iframe loading=\"lazy\" src=\"https:\/\/docs.google.com\/presentation\/d\/1jWcAHGoqWg5YrlphrbL1mll1-n-jEt8EgmbhnQrtc-U\/embed?start=false&amp;loop=false&amp;delayms=60000\" width=\"960\" height=\"569\" frameborder=\"0\" allowfullscreen=\"allowfullscreen\"><\/iframe><\/p>\n<\/div><\/div><\/div>\n\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"neve_meta_sidebar":"","neve_meta_container":"","neve_meta_enable_content_width":"","neve_meta_content_width":0,"neve_meta_title_alignment":"","neve_meta_author_avatar":"","neve_post_elements_order":"","neve_meta_disable_header":"","neve_meta_disable_footer":"","neve_meta_disable_title":""},"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.6 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Lists - Python<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/learnlearn.uk\/python\/lists\/\" \/>\n<meta property=\"og:locale\" content=\"en_GB\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Lists - Python\" \/>\n<meta property=\"og:url\" content=\"https:\/\/learnlearn.uk\/python\/lists\/\" \/>\n<meta property=\"og:site_name\" content=\"Python\" \/>\n<meta property=\"article:modified_time\" content=\"2017-01-22T10:19:26+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/learnlearn.uk\/python\/wp-content\/uploads\/sites\/4\/2016\/01\/yttinyiconblue.png?_t=1485080279\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Estimated reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/learnlearn.uk\/python\/lists\/\",\"url\":\"https:\/\/learnlearn.uk\/python\/lists\/\",\"name\":\"Lists - Python\",\"isPartOf\":{\"@id\":\"https:\/\/learnlearn.uk\/python\/#website\"},\"datePublished\":\"2016-01-06T15:54:43+00:00\",\"dateModified\":\"2017-01-22T10:19:26+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/learnlearn.uk\/python\/lists\/#breadcrumb\"},\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/learnlearn.uk\/python\/lists\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/learnlearn.uk\/python\/lists\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Python Unit Home\",\"item\":\"https:\/\/learnlearn.uk\/python\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Lists\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/learnlearn.uk\/python\/#website\",\"url\":\"https:\/\/learnlearn.uk\/python\/\",\"name\":\"Python\",\"description\":\"Programming\",\"publisher\":{\"@id\":\"https:\/\/learnlearn.uk\/python\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/learnlearn.uk\/python\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-GB\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/learnlearn.uk\/python\/#organization\",\"name\":\"Python\",\"url\":\"https:\/\/learnlearn.uk\/python\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-GB\",\"@id\":\"https:\/\/learnlearn.uk\/python\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/learnlearn.uk\/python\/wp-content\/uploads\/sites\/4\/2019\/03\/LearnLearnLogowhite.png\",\"contentUrl\":\"https:\/\/learnlearn.uk\/python\/wp-content\/uploads\/sites\/4\/2019\/03\/LearnLearnLogowhite.png\",\"width\":710,\"height\":98,\"caption\":\"Python\"},\"image\":{\"@id\":\"https:\/\/learnlearn.uk\/python\/#\/schema\/logo\/image\/\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Lists - Python","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/learnlearn.uk\/python\/lists\/","og_locale":"en_GB","og_type":"article","og_title":"Lists - Python","og_url":"https:\/\/learnlearn.uk\/python\/lists\/","og_site_name":"Python","article_modified_time":"2017-01-22T10:19:26+00:00","og_image":[{"url":"http:\/\/learnlearn.uk\/python\/wp-content\/uploads\/sites\/4\/2016\/01\/yttinyiconblue.png?_t=1485080279"}],"twitter_card":"summary_large_image","twitter_misc":{"Estimated reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/learnlearn.uk\/python\/lists\/","url":"https:\/\/learnlearn.uk\/python\/lists\/","name":"Lists - Python","isPartOf":{"@id":"https:\/\/learnlearn.uk\/python\/#website"},"datePublished":"2016-01-06T15:54:43+00:00","dateModified":"2017-01-22T10:19:26+00:00","breadcrumb":{"@id":"https:\/\/learnlearn.uk\/python\/lists\/#breadcrumb"},"inLanguage":"en-GB","potentialAction":[{"@type":"ReadAction","target":["https:\/\/learnlearn.uk\/python\/lists\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/learnlearn.uk\/python\/lists\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Python Unit Home","item":"https:\/\/learnlearn.uk\/python\/"},{"@type":"ListItem","position":2,"name":"Lists"}]},{"@type":"WebSite","@id":"https:\/\/learnlearn.uk\/python\/#website","url":"https:\/\/learnlearn.uk\/python\/","name":"Python","description":"Programming","publisher":{"@id":"https:\/\/learnlearn.uk\/python\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/learnlearn.uk\/python\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-GB"},{"@type":"Organization","@id":"https:\/\/learnlearn.uk\/python\/#organization","name":"Python","url":"https:\/\/learnlearn.uk\/python\/","logo":{"@type":"ImageObject","inLanguage":"en-GB","@id":"https:\/\/learnlearn.uk\/python\/#\/schema\/logo\/image\/","url":"https:\/\/learnlearn.uk\/python\/wp-content\/uploads\/sites\/4\/2019\/03\/LearnLearnLogowhite.png","contentUrl":"https:\/\/learnlearn.uk\/python\/wp-content\/uploads\/sites\/4\/2019\/03\/LearnLearnLogowhite.png","width":710,"height":98,"caption":"Python"},"image":{"@id":"https:\/\/learnlearn.uk\/python\/#\/schema\/logo\/image\/"}}]}},"rttpg_featured_image_url":null,"rttpg_author":{"display_name":"learnlearnadmin","author_link":"https:\/\/learnlearn.uk\/python\/author\/learnlearnadmin\/"},"rttpg_comment":0,"rttpg_category":null,"rttpg_excerpt":null,"_links":{"self":[{"href":"https:\/\/learnlearn.uk\/python\/wp-json\/wp\/v2\/pages\/13"}],"collection":[{"href":"https:\/\/learnlearn.uk\/python\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/learnlearn.uk\/python\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/learnlearn.uk\/python\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learnlearn.uk\/python\/wp-json\/wp\/v2\/comments?post=13"}],"version-history":[{"count":0,"href":"https:\/\/learnlearn.uk\/python\/wp-json\/wp\/v2\/pages\/13\/revisions"}],"wp:attachment":[{"href":"https:\/\/learnlearn.uk\/python\/wp-json\/wp\/v2\/media?parent=13"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}