{"id":551,"date":"2023-02-06T01:19:47","date_gmt":"2023-02-06T01:19:47","guid":{"rendered":"http:\/\/yesrenee.com\/?p=551"},"modified":"2023-08-28T18:02:29","modified_gmt":"2023-08-28T18:02:29","slug":"python-lists","status":"publish","type":"post","link":"https:\/\/yesrenee.com\/?p=551","title":{"rendered":"Python Lists"},"content":{"rendered":"\n<p>Python lists can be extremely useful. <\/p>\n\n\n\n<p>What is a list? A list holds elements together. Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>g = &#91;'hi', 'hello', 'how are you', 'good day', 'how are you doing']\n<\/code><\/pre>\n\n\n\n<p>The element &#8220;hi&#8221; is <em>element 0<\/em>. &#8220;hello&#8221; is number 1. So the first element we see is <em>actually<\/em> the <em>0th<\/em> element in Python.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>What you can do to a list:<\/p>\n\n\n\n<p>(result is below)<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>g = &#91;'hi', 'hello', 'how are you', 'good day', 'how are you doing']\nprint(g&#91;0]) #print the first element in greetings<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>hi<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>g = &#91;'hi', 'hello', 'how are you', 'good evening', 'good afternoon']\ng&#91;1] = 'good morning'  #switch element 1 (hello) with \"good morning\"\nprint(g)<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;'hi', 'good morning', 'how are you', 'good evening', 'good afternoon']<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>g = &#91;'hi', 'good morning', 'how are you', 'good evening', 'good afternoon']\ng.append('hey')  #add something\nprint(g)<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;'hi', 'good morning', 'how are you', 'good evening', 'good afternoon', 'hey']<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>g = &#91;'hi', 'good morning', 'how are you', 'good evening', 'good afternoon', 'hey']\ng.remove('how are you')  #remove something\nprint(g)<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;'hi', 'good morning', 'good evening', 'good afternoon', 'hey']<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>g = &#91;'hi', 'good morning', 'how are you', 'good evening', 'good afternoon', 'hey']\ng.sort()  #sort in alphabetical order\nprint(g)<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;'good afternoon', 'good evening', 'good morning', 'hey', 'hi', 'how are you']\n<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>Also:<br><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>s = &#91;1, 2, 3, 4, 5]\na = sum(s)\nprint(a)<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>15<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python lists can be extremely useful. What is a list? A list holds elements together. Example: The element &#8220;hi&#8221; is element 0. &#8220;hello&#8221; is number 1. So the first element we see is actually the 0th element in Python. What you can do to a list: (result is below) Also:<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[19],"class_list":["post-551","post","type-post","status-publish","format-standard","hentry","category-daily","tag-python-programming"],"_links":{"self":[{"href":"https:\/\/yesrenee.com\/index.php?rest_route=\/wp\/v2\/posts\/551","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/yesrenee.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/yesrenee.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/yesrenee.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/yesrenee.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=551"}],"version-history":[{"count":2,"href":"https:\/\/yesrenee.com\/index.php?rest_route=\/wp\/v2\/posts\/551\/revisions"}],"predecessor-version":[{"id":554,"href":"https:\/\/yesrenee.com\/index.php?rest_route=\/wp\/v2\/posts\/551\/revisions\/554"}],"wp:attachment":[{"href":"https:\/\/yesrenee.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=551"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/yesrenee.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=551"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/yesrenee.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=551"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}