{"id":224,"date":"2019-12-26T23:16:00","date_gmt":"2019-12-26T23:16:00","guid":{"rendered":"https:\/\/zofxare.com\/zofxare\/blog\/2019\/12\/26\/python-tutorial-series-lists\/"},"modified":"2019-12-26T23:16:00","modified_gmt":"2019-12-26T23:16:00","slug":"python-tutorial-series-lists","status":"publish","type":"post","link":"https:\/\/zofxare.com\/zofxare\/blog\/2019\/12\/26\/python-tutorial-series-lists\/","title":{"rendered":"Python Tutorial Series &#8211; Lists"},"content":{"rendered":"<p>Python provides support for several types of collections. A collection holds a group of items. An item may be a number, string, object or another collection.<\/p>\n<p>One Python collection type is a list. Each item in the list may be a different data type.<\/p>\n<p><strong>Lists<\/strong><\/p>\n<p>Lists are used to store comma separated values enclosed in brackets, example:<\/p>\n<pre>to_do_list = ['groceries', 'laundry', 'wash car', 'vacuum']<\/pre>\n<p>\nLists retain their order and elements in a list may be changed.<br \/>\nList elements have a corresponding index value. Indexing starts at 0 and increases by 1. Elements are accessed by using their index value.<br \/>\nFor example this would print the third element value in the list, which is &#8216;wash car&#8217;:<\/p>\n<pre>print(to_do_list[2])<\/pre>\n<p>\nLists may also be indexed with negative values starting at the last element. Reverse indexing starts at &#8211; 1 and decreases by -1. For example, this would also print the third element value in the list, which is &#8216;wash car&#8217;:<\/p>\n<pre>print(to_do_list[-2])<\/pre>\n<p>\nLists may be looped through by using the in keyword:<\/p>\n<pre>for item in to_do_list:<\/pre>\n<p>\n&nbsp; &nbsp; print(item)<\/p>\n<p>Python provides support for many built in list functions. These functions are as follows:<\/p>\n<p><strong>append<\/strong> &#8211; Adds an element to the end of a list. Example: <\/p>\n<pre>to_do_list.append('exercise')<\/pre>\n<p>\n<strong>clear <\/strong> &#8211; Removes all elements from the list. Example: <\/p>\n<pre>to_do_list.clear()<\/pre>\n<p>\n<strong>copy <\/strong> &#8211; Returns a copy of the list. Example: <\/p>\n<pre>new_list = to_do_list.copy()<\/pre>\n<p>\n<strong>count <\/strong> &#8211; Returns the number of elements in the list with the given input value. Example: <\/p>\n<pre>to_do_list.count('vacuum')<\/pre>\n<p>\n<strong>extend <\/strong> &#8211; Adds to the end of a list, another list, set, tuple, etc. Example: <\/p>\n<pre>to_do_list.extend(['wash dishes', 'take nap'])<\/pre>\n<p>\n<strong>index <\/strong> &#8211; Returns the index of the input value. Example: <\/p>\n<pre>to_do_list.index('vacuum')<\/pre>\n<p>\n<strong>insert <\/strong> &#8211; Adds an element to the list in the given index position. Example: <\/p>\n<pre>to_do_list.insert(1, 'shovel snow')<\/pre>\n<p>which becomes:<br \/>\nto_do_list = [&#8216;groceries&#8217;, &#8216;shovel snow&#8217;, &#8216;laundry&#8217;, &#8216;wash car&#8217;, &#8216;vacuum&#8217;]<\/p>\n<p><strong>pop <\/strong> &#8211; Removes an item from the list with the input index. Example: <\/p>\n<pre>to_do_list.pop(2)<\/pre>\n<p>\n<strong>remove<\/strong> &#8211; Removes an item from the list with the input value. Example: <\/p>\n<pre>to_do_list.remove('vacuum')<\/pre>\n<p>\n<strong>reverse<\/strong> &#8211; Returns the list in reverse. Example: <\/p>\n<pre>to_do_list.reverse()<\/pre>\n<p>\n<strong>sort<\/strong> &#8211; Sorts a list alphabetically. Example: <\/p>\n<pre>to_do_list.sort()<\/pre>\n<p>\nSorts a list descending. Example: <\/p>\n<pre>to_do_list.sort(false)<\/pre>\n<p>\nA custom sort function may also be defined. Example:<\/p>\n<pre><\/pre>\n<p>\ndef custom(element):<br \/>\n&nbsp; &nbsp; return element[0]<\/p>\n<p>to_do_list.sort(key=custom)<\/p>\n<p>Returns: [&#8216;groceries&#8217;, &#8216;laundry&#8217;, &#8216;shovel snow&#8217;, &#8216;vacuum&#8217;, &#8216;wash car&#8217;]<\/p>\n<p>A couple of other useful Python methods for lists are:<\/p>\n<p><strong>del<\/strong> &#8211; Delete an element from a list. This does not return a value. Example: <\/p>\n<pre>del to_do_list[2]<\/pre>\n<p>\n&nbsp; &nbsp; This deletes the entire list: <\/p>\n<pre>del to_do_list<\/pre>\n<p>\n<strong>len<\/strong> &#8211; Returns the number of elements in a list. Example: <\/p>\n<pre>num = len(to_do_list)<\/pre>\n<p>\n<strong>list<\/strong> &#8211; Constructor used to create a list. Example: <\/p>\n<pre>new_list = list(('exercise', 'shovel snow'))<\/pre>\n<p>\n<strong>min<\/strong> &#8211; Returns the item with the minimum value. Example: <\/p>\n<pre>to_do_tuple.min() # returns 'groceries'<\/pre>\n<p>\n<strong>max<\/strong> &#8211; Returns the item with the maximum value. Example: <\/p>\n<pre>to_do_tuple.max() # returns 'wash car'<\/pre>\n<p><strong>Slicing<\/strong><\/p>\n<p>Ranges of list items can be accessed by slicing. This is done by indexing the list with a beginning index followed by a colon and an ending index. The beginning index is inclusive. The ending index is exclusive. Example:<\/p>\n<p>Extract elements 2 &#8211; 3 which would be indexes 1 &#8211; 2.<\/p>\n<pre>extracted_list = to_do_list[1:3]<\/pre>\n<p>Using the original list, this returns: to_do_list[&#8216;laundry&#8217;, &#8216;wash car&#8217;] which is:<br \/>\n[element 2, element 3] which is:<br \/>\n[index 1, index 2]<\/p>\n<p>If the beginning index is not included, the slicing starts at the first element in the list.<\/p>\n<pre>extracted_list = to_do_list[:3] # returns to_do_list['groceries', 'laundry', 'wash car']<\/pre>\n<p>If the ending index is not included, the slicing ends at the last element in the list.<\/p>\n<pre>extracted_list = to_do_list[1:] # returns to_do_list['laundry', 'wash car', 'vacuum']<\/pre>\n<p>If neither the beginning index or ending index is included, the slicing starts at the first element and ends at the last element.<\/p>\n<pre>extracted_list = to_do_list[:] # returns to_do_list['groceries', 'laundry', 'wash car', 'vacuum']<\/pre>\n<p><a href=\"https:\/\/amzn.to\/2EimtFd\" rel=\"noopener\" target=\"_blank\">More Python<\/a><\/p>\n<p><iframe frameborder=\"0\" marginheight=\"0\" marginwidth=\"0\" scrolling=\"no\" src=\"\/\/ws-na.amazon-adsystem.com\/widgets\/q?ServiceVersion=20070822&amp;OneJS=1&amp;Operation=GetAdHtml&amp;MarketPlace=US&amp;source=ss&amp;ref=as_ss_li_til&amp;ad_type=product_link&amp;tracking_id=zofxare-20&amp;language=en_US&amp;marketplace=amazon&amp;region=US&amp;placement=1593276036&amp;asins=1593276036&amp;linkId=49e81436e850d4eb917cf05eb7bb46a9&amp;show_border=true&amp;link_opens_in_new_window=true\" style=\"height: 240px; width: 120px;\"><\/iframe><iframe frameborder=\"0\" marginheight=\"0\" marginwidth=\"0\" scrolling=\"no\" src=\"\/\/ws-na.amazon-adsystem.com\/widgets\/q?ServiceVersion=20070822&amp;OneJS=1&amp;Operation=GetAdHtml&amp;MarketPlace=US&amp;source=ss&amp;ref=as_ss_li_til&amp;ad_type=product_link&amp;tracking_id=zofxare-20&amp;language=en_US&amp;marketplace=amazon&amp;region=US&amp;placement=1631595814&amp;asins=1631595814&amp;linkId=ab04bfbaa6f7474875f3b72a0ce3fc64&amp;show_border=true&amp;link_opens_in_new_window=true\" style=\"height: 240px; width: 120px;\"><\/iframe><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python provides support for several types of collections. A collection holds a group of items. An item may be a number, string, object or another collection. One Python collection type is a list. Each item in the list may be a different data type. Lists Lists are used to store comma separated values enclosed in [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"nf_dc_page":"","om_disable_all_campaigns":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[13,343,341],"tags":[],"class_list":["post-224","post","type-post","status-publish","format-standard","hentry","category-python","category-python-tutorials","category-tutorials"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Python Tutorial Series - Lists - Zofxare Blog Home<\/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:\/\/zofxare.com\/zofxare\/blog\/2019\/12\/26\/python-tutorial-series-lists\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Tutorial Series - Lists - Zofxare Blog Home\" \/>\n<meta property=\"og:description\" content=\"Python provides support for several types of collections. A collection holds a group of items. An item may be a number, string, object or another collection. One Python collection type is a list. Each item in the list may be a different data type. Lists Lists are used to store comma separated values enclosed in [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/zofxare.com\/zofxare\/blog\/2019\/12\/26\/python-tutorial-series-lists\/\" \/>\n<meta property=\"og:site_name\" content=\"Zofxare Blog Home\" \/>\n<meta property=\"article:published_time\" content=\"2019-12-26T23:16:00+00:00\" \/>\n<meta name=\"author\" content=\"adminuser\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"adminuser\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/2019\\\/12\\\/26\\\/python-tutorial-series-lists\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/2019\\\/12\\\/26\\\/python-tutorial-series-lists\\\/\"},\"author\":{\"name\":\"adminuser\",\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/#\\\/schema\\\/person\\\/488c98b837076eb349075c14ea0e87d8\"},\"headline\":\"Python Tutorial Series &#8211; Lists\",\"datePublished\":\"2019-12-26T23:16:00+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/2019\\\/12\\\/26\\\/python-tutorial-series-lists\\\/\"},\"wordCount\":550,\"commentCount\":0,\"articleSection\":[\"python\",\"python tutorials\",\"tutorials\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/2019\\\/12\\\/26\\\/python-tutorial-series-lists\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/2019\\\/12\\\/26\\\/python-tutorial-series-lists\\\/\",\"url\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/2019\\\/12\\\/26\\\/python-tutorial-series-lists\\\/\",\"name\":\"Python Tutorial Series - Lists - Zofxare Blog Home\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/#website\"},\"datePublished\":\"2019-12-26T23:16:00+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/#\\\/schema\\\/person\\\/488c98b837076eb349075c14ea0e87d8\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/2019\\\/12\\\/26\\\/python-tutorial-series-lists\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/2019\\\/12\\\/26\\\/python-tutorial-series-lists\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/2019\\\/12\\\/26\\\/python-tutorial-series-lists\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python Tutorial Series &#8211; Lists\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/\",\"name\":\"Zofxare Blog Home\",\"description\":\"The Zofxare blog\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/#\\\/schema\\\/person\\\/488c98b837076eb349075c14ea0e87d8\",\"name\":\"adminuser\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/c5fe9cf8bb25011247f8063d1c50de6fbdd21be02889559dd151d722f050f037?s=96&d=retro&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/c5fe9cf8bb25011247f8063d1c50de6fbdd21be02889559dd151d722f050f037?s=96&d=retro&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/c5fe9cf8bb25011247f8063d1c50de6fbdd21be02889559dd151d722f050f037?s=96&d=retro&r=g\",\"caption\":\"adminuser\"},\"sameAs\":[\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\"],\"url\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/author\\\/adminuser\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python Tutorial Series - Lists - Zofxare Blog Home","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:\/\/zofxare.com\/zofxare\/blog\/2019\/12\/26\/python-tutorial-series-lists\/","og_locale":"en_US","og_type":"article","og_title":"Python Tutorial Series - Lists - Zofxare Blog Home","og_description":"Python provides support for several types of collections. A collection holds a group of items. An item may be a number, string, object or another collection. One Python collection type is a list. Each item in the list may be a different data type. Lists Lists are used to store comma separated values enclosed in [&hellip;]","og_url":"https:\/\/zofxare.com\/zofxare\/blog\/2019\/12\/26\/python-tutorial-series-lists\/","og_site_name":"Zofxare Blog Home","article_published_time":"2019-12-26T23:16:00+00:00","author":"adminuser","twitter_card":"summary_large_image","twitter_misc":{"Written by":"adminuser","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/zofxare.com\/zofxare\/blog\/2019\/12\/26\/python-tutorial-series-lists\/#article","isPartOf":{"@id":"https:\/\/zofxare.com\/zofxare\/blog\/2019\/12\/26\/python-tutorial-series-lists\/"},"author":{"name":"adminuser","@id":"https:\/\/zofxare.com\/zofxare\/blog\/#\/schema\/person\/488c98b837076eb349075c14ea0e87d8"},"headline":"Python Tutorial Series &#8211; Lists","datePublished":"2019-12-26T23:16:00+00:00","mainEntityOfPage":{"@id":"https:\/\/zofxare.com\/zofxare\/blog\/2019\/12\/26\/python-tutorial-series-lists\/"},"wordCount":550,"commentCount":0,"articleSection":["python","python tutorials","tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/zofxare.com\/zofxare\/blog\/2019\/12\/26\/python-tutorial-series-lists\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/zofxare.com\/zofxare\/blog\/2019\/12\/26\/python-tutorial-series-lists\/","url":"https:\/\/zofxare.com\/zofxare\/blog\/2019\/12\/26\/python-tutorial-series-lists\/","name":"Python Tutorial Series - Lists - Zofxare Blog Home","isPartOf":{"@id":"https:\/\/zofxare.com\/zofxare\/blog\/#website"},"datePublished":"2019-12-26T23:16:00+00:00","author":{"@id":"https:\/\/zofxare.com\/zofxare\/blog\/#\/schema\/person\/488c98b837076eb349075c14ea0e87d8"},"breadcrumb":{"@id":"https:\/\/zofxare.com\/zofxare\/blog\/2019\/12\/26\/python-tutorial-series-lists\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/zofxare.com\/zofxare\/blog\/2019\/12\/26\/python-tutorial-series-lists\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/zofxare.com\/zofxare\/blog\/2019\/12\/26\/python-tutorial-series-lists\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/zofxare.com\/zofxare\/blog\/"},{"@type":"ListItem","position":2,"name":"Python Tutorial Series &#8211; Lists"}]},{"@type":"WebSite","@id":"https:\/\/zofxare.com\/zofxare\/blog\/#website","url":"https:\/\/zofxare.com\/zofxare\/blog\/","name":"Zofxare Blog Home","description":"The Zofxare blog","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/zofxare.com\/zofxare\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/zofxare.com\/zofxare\/blog\/#\/schema\/person\/488c98b837076eb349075c14ea0e87d8","name":"adminuser","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/c5fe9cf8bb25011247f8063d1c50de6fbdd21be02889559dd151d722f050f037?s=96&d=retro&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/c5fe9cf8bb25011247f8063d1c50de6fbdd21be02889559dd151d722f050f037?s=96&d=retro&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c5fe9cf8bb25011247f8063d1c50de6fbdd21be02889559dd151d722f050f037?s=96&d=retro&r=g","caption":"adminuser"},"sameAs":["https:\/\/zofxare.com\/zofxare\/blog"],"url":"https:\/\/zofxare.com\/zofxare\/blog\/author\/adminuser\/"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/zofxare.com\/zofxare\/blog\/wp-json\/wp\/v2\/posts\/224","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zofxare.com\/zofxare\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zofxare.com\/zofxare\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zofxare.com\/zofxare\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zofxare.com\/zofxare\/blog\/wp-json\/wp\/v2\/comments?post=224"}],"version-history":[{"count":0,"href":"https:\/\/zofxare.com\/zofxare\/blog\/wp-json\/wp\/v2\/posts\/224\/revisions"}],"wp:attachment":[{"href":"https:\/\/zofxare.com\/zofxare\/blog\/wp-json\/wp\/v2\/media?parent=224"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zofxare.com\/zofxare\/blog\/wp-json\/wp\/v2\/categories?post=224"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zofxare.com\/zofxare\/blog\/wp-json\/wp\/v2\/tags?post=224"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}