{"id":1267,"date":"2020-04-16T11:19:01","date_gmt":"2020-04-16T18:19:01","guid":{"rendered":"https:\/\/renanmf.com\/?p=1267"},"modified":"2020-12-29T10:24:11","modified_gmt":"2020-12-29T13:24:11","slug":"python-list","status":"publish","type":"post","link":"https:\/\/renanmf.com\/python-list\/","title":{"rendered":"Python List: a quick reference"},"content":{"rendered":"<p>A list has its items ordered and you can add the same item as many times as you want.<\/p>\n<p>An important detail is that lists are mutable.<\/p>\n<h3>Initialization<\/h3>\n<h4>Empty List<\/h4>\n<pre><code class=\"language-python\">people = []<\/code><\/pre>\n<h4>List with initial values<\/h4>\n<pre><code class=\"language-python\">people = [&#039;Bob&#039;, &#039;Mary&#039;]<\/code><\/pre>\n<h3>Adding in a List<\/h3>\n<p>To add an item in the end of a list, use <code>append()<\/code>.<\/p>\n<pre><code class=\"language-python\">people = [&#039;Bob&#039;, &#039;Mary&#039;]\npeople.append(&#039;Sarah&#039;)\n\nprint(people)<\/code><\/pre>\n<pre><code>[&#039;Bob&#039;, &#039;Mary&#039;, &#039;Sarah&#039;]<\/code><\/pre>\n<p>To specify the position for the new item, use the <code>insert()<\/code> method.<\/p>\n<pre><code class=\"language-python\">people = [&#039;Bob&#039;, &#039;Mary&#039;]\npeople.insert(0, &#039;Sarah&#039;)\n\nprint(people)<\/code><\/pre>\n<pre><code>[&#039;Sarah&#039;, &#039;Bob&#039;, &#039;Mary&#039;]<\/code><\/pre>\n<h3>Updating in a List<\/h3>\n<p>Specify the position of the item to update and set the new value<\/p>\n<pre><code class=\"language-python\">people = [&#039;Bob&#039;, &#039;Mary&#039;]\npeople[1] = &#039;Sarah&#039;\nprint(people)<\/code><\/pre>\n<pre><code>[&#039;Bob&#039;, &#039;Sarah&#039;]<\/code><\/pre>\n<h3>Deleting in a List<\/h3>\n<p>Use the <code>remove()<\/code> method to delete the item given as an argument.<\/p>\n<pre><code class=\"language-python\">people = [&#039;Bob&#039;, &#039;Mary&#039;]\npeople.remove(&#039;Bob&#039;)\nprint(people)<\/code><\/pre>\n<pre><code>[&#039;Mary&#039;]<\/code><\/pre>\n<p>To delete everybody, use the <code>clear()<\/code> method:<\/p>\n<pre><code class=\"language-python\">people = [&#039;Bob&#039;, &#039;Mary&#039;]\npeople.clear()<\/code><\/pre>\n<h3>Retrieving in a List<\/h3>\n<p>Use the index to reference the item.<\/p>\n<p>Remember that the index starts at 0.<\/p>\n<p>So to access the second item use the index 1.<\/p>\n<pre><code class=\"language-python\">people = [&#039;Bob&#039;, &#039;Mary&#039;]\nprint(people[1])<\/code><\/pre>\n<pre><code>Mary<\/code><\/pre>\n<h3>Iterating over Lists<\/h3>\n<p>To print the keys:<\/p>\n<pre><code class=\"language-python\">people = [&#039;Bob&#039;, &#039;Mary&#039;]\n\nfor person in people:\n  print(person)<\/code><\/pre>\n<pre><code>Bob\nMary<\/code><\/pre>\n<h3>Check if a given item already exists in a List<\/h3>\n<pre><code class=\"language-python\">people = [&#039;Bob&#039;, &#039;Mary&#039;]\n\nif &#039;Bob&#039; in people:\n  print(&#039;Bob exists!&#039;)\nelse:\n  print(&#039;There is no Bob!&#039;)<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>A list has its items ordered and you can add the same item as many times as you want. An important detail is that lists are mutable. Initialization Empty List people = [] List with initial values people = [&#039;Bob&#039;, &#039;Mary&#039;] Adding in a List To add an item in the end of a list, [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":742,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[6],"class_list":["post-1267","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-python"],"blocksy_meta":{"styles_descriptor":{"styles":{"desktop":"","tablet":"","mobile":""},"google_fonts":[],"version":6}},"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Python List: a quick reference<\/title>\n<meta name=\"description\" content=\"In this tutorial I show how easy it is to work with lists in Python.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/renanmf.com\/python-list\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python List: a quick reference\" \/>\n<meta property=\"og:description\" content=\"In this tutorial I show how easy it is to work with lists in Python.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/renanmf.com\/python-list\/\" \/>\n<meta property=\"og:site_name\" content=\"Renan Moura - Software Engineering\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/renanmouraf\" \/>\n<meta property=\"article:published_time\" content=\"2020-04-16T18:19:01+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-12-29T13:24:11+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/renanmf.com\/wp-content\/uploads\/2020\/02\/fig-27-02-2020_21-01-52.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1800\" \/>\n\t<meta property=\"og:image:height\" content=\"1200\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Renan Moura\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/twitter.com\/renanmouraf\" \/>\n<meta name=\"twitter:site\" content=\"@renanmouraf\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Renan Moura\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/renanmf.com\\\/python-list\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/renanmf.com\\\/python-list\\\/\"},\"author\":{\"name\":\"Renan Moura\",\"@id\":\"https:\\\/\\\/renanmf.com\\\/#\\\/schema\\\/person\\\/1a6fd46256318d200c1c8a867448e5a8\"},\"headline\":\"Python List: a quick reference\",\"datePublished\":\"2020-04-16T18:19:01+00:00\",\"dateModified\":\"2020-12-29T13:24:11+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/renanmf.com\\\/python-list\\\/\"},\"wordCount\":146,\"publisher\":{\"@id\":\"https:\\\/\\\/renanmf.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/renanmf.com\\\/python-list\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/renanmf.com\\\/wp-content\\\/uploads\\\/2020\\\/02\\\/fig-27-02-2020_21-01-52.jpg\",\"keywords\":[\"python\"],\"articleSection\":[\"Python\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/renanmf.com\\\/python-list\\\/\",\"url\":\"https:\\\/\\\/renanmf.com\\\/python-list\\\/\",\"name\":\"Python List: a quick reference\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/renanmf.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/renanmf.com\\\/python-list\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/renanmf.com\\\/python-list\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/renanmf.com\\\/wp-content\\\/uploads\\\/2020\\\/02\\\/fig-27-02-2020_21-01-52.jpg\",\"datePublished\":\"2020-04-16T18:19:01+00:00\",\"dateModified\":\"2020-12-29T13:24:11+00:00\",\"description\":\"In this tutorial I show how easy it is to work with lists in Python.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/renanmf.com\\\/python-list\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/renanmf.com\\\/python-list\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/renanmf.com\\\/python-list\\\/#primaryimage\",\"url\":\"https:\\\/\\\/renanmf.com\\\/wp-content\\\/uploads\\\/2020\\\/02\\\/fig-27-02-2020_21-01-52.jpg\",\"contentUrl\":\"https:\\\/\\\/renanmf.com\\\/wp-content\\\/uploads\\\/2020\\\/02\\\/fig-27-02-2020_21-01-52.jpg\",\"width\":1800,\"height\":1200,\"caption\":\"how to python\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/renanmf.com\\\/python-list\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"In\u00edcio\",\"item\":\"https:\\\/\\\/renanmf.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python List: a quick reference\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/renanmf.com\\\/#website\",\"url\":\"https:\\\/\\\/renanmf.com\\\/\",\"name\":\"Renan Moura - Software Engineering\",\"description\":\"Software development, machine learning\",\"publisher\":{\"@id\":\"https:\\\/\\\/renanmf.com\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/renanmf.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/renanmf.com\\\/#organization\",\"name\":\"Renan Moura\",\"url\":\"https:\\\/\\\/renanmf.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/renanmf.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/renanmf.com\\\/wp-content\\\/uploads\\\/2020\\\/03\\\/me-e1583179172701.jpeg\",\"contentUrl\":\"https:\\\/\\\/renanmf.com\\\/wp-content\\\/uploads\\\/2020\\\/03\\\/me-e1583179172701.jpeg\",\"width\":120,\"height\":120,\"caption\":\"Renan Moura\"},\"image\":{\"@id\":\"https:\\\/\\\/renanmf.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/renanmouraf\",\"https:\\\/\\\/x.com\\\/renanmouraf\",\"https:\\\/\\\/instagram.com\\\/renanmouraf\",\"https:\\\/\\\/www.linkedin.com\\\/in\\\/renanmouraf\\\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/renanmf.com\\\/#\\\/schema\\\/person\\\/1a6fd46256318d200c1c8a867448e5a8\",\"name\":\"Renan Moura\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/efb78bdd04aa5627f80307aed5a9b31989d901c536d1e014a29a3c3591338af8?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/efb78bdd04aa5627f80307aed5a9b31989d901c536d1e014a29a3c3591338af8?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/efb78bdd04aa5627f80307aed5a9b31989d901c536d1e014a29a3c3591338af8?s=96&d=mm&r=g\",\"caption\":\"Renan Moura\"},\"description\":\"I'm a Software Engineer working in the industry for a decade now. I like to solve problems with as little code as possible. I\u2019m interested in solving all sorts of problems with technology in creative and innovative ways. From everyday shell scripts to machine learning models. I write about Software Development, Machine Learning, and Career in tech.\",\"sameAs\":[\"https:\\\/\\\/renanmf.com\\\/\",\"https:\\\/\\\/www.instagram.com\\\/renanmouraf\\\/\",\"https:\\\/\\\/www.linkedin.com\\\/in\\\/renanmouraf\\\/\",\"https:\\\/\\\/x.com\\\/https:\\\/\\\/twitter.com\\\/renanmouraf\"],\"url\":\"https:\\\/\\\/renanmf.com\\\/author\\\/renanmoura\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python List: a quick reference","description":"In this tutorial I show how easy it is to work with lists in 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:\/\/renanmf.com\/python-list\/","og_locale":"en_US","og_type":"article","og_title":"Python List: a quick reference","og_description":"In this tutorial I show how easy it is to work with lists in Python.","og_url":"https:\/\/renanmf.com\/python-list\/","og_site_name":"Renan Moura - Software Engineering","article_publisher":"https:\/\/www.facebook.com\/renanmouraf","article_published_time":"2020-04-16T18:19:01+00:00","article_modified_time":"2020-12-29T13:24:11+00:00","og_image":[{"width":1800,"height":1200,"url":"https:\/\/renanmf.com\/wp-content\/uploads\/2020\/02\/fig-27-02-2020_21-01-52.jpg","type":"image\/jpeg"}],"author":"Renan Moura","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/twitter.com\/renanmouraf","twitter_site":"@renanmouraf","twitter_misc":{"Written by":"Renan Moura","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/renanmf.com\/python-list\/#article","isPartOf":{"@id":"https:\/\/renanmf.com\/python-list\/"},"author":{"name":"Renan Moura","@id":"https:\/\/renanmf.com\/#\/schema\/person\/1a6fd46256318d200c1c8a867448e5a8"},"headline":"Python List: a quick reference","datePublished":"2020-04-16T18:19:01+00:00","dateModified":"2020-12-29T13:24:11+00:00","mainEntityOfPage":{"@id":"https:\/\/renanmf.com\/python-list\/"},"wordCount":146,"publisher":{"@id":"https:\/\/renanmf.com\/#organization"},"image":{"@id":"https:\/\/renanmf.com\/python-list\/#primaryimage"},"thumbnailUrl":"https:\/\/renanmf.com\/wp-content\/uploads\/2020\/02\/fig-27-02-2020_21-01-52.jpg","keywords":["python"],"articleSection":["Python"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/renanmf.com\/python-list\/","url":"https:\/\/renanmf.com\/python-list\/","name":"Python List: a quick reference","isPartOf":{"@id":"https:\/\/renanmf.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/renanmf.com\/python-list\/#primaryimage"},"image":{"@id":"https:\/\/renanmf.com\/python-list\/#primaryimage"},"thumbnailUrl":"https:\/\/renanmf.com\/wp-content\/uploads\/2020\/02\/fig-27-02-2020_21-01-52.jpg","datePublished":"2020-04-16T18:19:01+00:00","dateModified":"2020-12-29T13:24:11+00:00","description":"In this tutorial I show how easy it is to work with lists in Python.","breadcrumb":{"@id":"https:\/\/renanmf.com\/python-list\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/renanmf.com\/python-list\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/renanmf.com\/python-list\/#primaryimage","url":"https:\/\/renanmf.com\/wp-content\/uploads\/2020\/02\/fig-27-02-2020_21-01-52.jpg","contentUrl":"https:\/\/renanmf.com\/wp-content\/uploads\/2020\/02\/fig-27-02-2020_21-01-52.jpg","width":1800,"height":1200,"caption":"how to python"},{"@type":"BreadcrumbList","@id":"https:\/\/renanmf.com\/python-list\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"In\u00edcio","item":"https:\/\/renanmf.com\/"},{"@type":"ListItem","position":2,"name":"Python List: a quick reference"}]},{"@type":"WebSite","@id":"https:\/\/renanmf.com\/#website","url":"https:\/\/renanmf.com\/","name":"Renan Moura - Software Engineering","description":"Software development, machine learning","publisher":{"@id":"https:\/\/renanmf.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/renanmf.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/renanmf.com\/#organization","name":"Renan Moura","url":"https:\/\/renanmf.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/renanmf.com\/#\/schema\/logo\/image\/","url":"https:\/\/renanmf.com\/wp-content\/uploads\/2020\/03\/me-e1583179172701.jpeg","contentUrl":"https:\/\/renanmf.com\/wp-content\/uploads\/2020\/03\/me-e1583179172701.jpeg","width":120,"height":120,"caption":"Renan Moura"},"image":{"@id":"https:\/\/renanmf.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/renanmouraf","https:\/\/x.com\/renanmouraf","https:\/\/instagram.com\/renanmouraf","https:\/\/www.linkedin.com\/in\/renanmouraf\/"]},{"@type":"Person","@id":"https:\/\/renanmf.com\/#\/schema\/person\/1a6fd46256318d200c1c8a867448e5a8","name":"Renan Moura","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/efb78bdd04aa5627f80307aed5a9b31989d901c536d1e014a29a3c3591338af8?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/efb78bdd04aa5627f80307aed5a9b31989d901c536d1e014a29a3c3591338af8?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/efb78bdd04aa5627f80307aed5a9b31989d901c536d1e014a29a3c3591338af8?s=96&d=mm&r=g","caption":"Renan Moura"},"description":"I'm a Software Engineer working in the industry for a decade now. I like to solve problems with as little code as possible. I\u2019m interested in solving all sorts of problems with technology in creative and innovative ways. From everyday shell scripts to machine learning models. I write about Software Development, Machine Learning, and Career in tech.","sameAs":["https:\/\/renanmf.com\/","https:\/\/www.instagram.com\/renanmouraf\/","https:\/\/www.linkedin.com\/in\/renanmouraf\/","https:\/\/x.com\/https:\/\/twitter.com\/renanmouraf"],"url":"https:\/\/renanmf.com\/author\/renanmoura\/"}]}},"_links":{"self":[{"href":"https:\/\/renanmf.com\/wp-json\/wp\/v2\/posts\/1267","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/renanmf.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/renanmf.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/renanmf.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/renanmf.com\/wp-json\/wp\/v2\/comments?post=1267"}],"version-history":[{"count":4,"href":"https:\/\/renanmf.com\/wp-json\/wp\/v2\/posts\/1267\/revisions"}],"predecessor-version":[{"id":2632,"href":"https:\/\/renanmf.com\/wp-json\/wp\/v2\/posts\/1267\/revisions\/2632"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/renanmf.com\/wp-json\/wp\/v2\/media\/742"}],"wp:attachment":[{"href":"https:\/\/renanmf.com\/wp-json\/wp\/v2\/media?parent=1267"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/renanmf.com\/wp-json\/wp\/v2\/categories?post=1267"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/renanmf.com\/wp-json\/wp\/v2\/tags?post=1267"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}