{"id":109986,"date":"2020-03-06T09:21:04","date_gmt":"2020-03-06T09:21:04","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=6579"},"modified":"2020-03-06T09:21:04","modified_gmt":"2020-03-06T09:21:04","slug":"python-list-methods","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2020\/03\/06\/python-list-methods\/","title":{"rendered":"Python List Methods"},"content":{"rendered":"<p>The most important collection data type in Python is the <code>list<\/code> data type. You&#8217;ll use lists basically in all your future projects so take 3-5 minutes and study this short guide carefully.<\/p>\n<p>You can also play my short video tutorial as you read over the methods:<\/p>\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\">\n<div class=\"wp-block-embed__wrapper\">\n<div class=\"ast-oembed-container\"><iframe loading=\"lazy\" title=\"Python List Methods\" width=\"1400\" height=\"788\" src=\"https:\/\/www.youtube.com\/embed\/4j8B9a_kZY4?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen><\/iframe><\/div>\n<\/div>\n<\/figure>\n<figure class=\"wp-block-table is-style-stripes\">\n<table>\n<thead>\n<tr>\n<th>Method<\/th>\n<th>Description<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><code>lst.append(x)<\/code><\/td>\n<td>Appends element <code>x<\/code> to the list <code>lst<\/code>.<\/td>\n<\/tr>\n<tr>\n<td><code><code>lst<\/code>.clear()<\/code><\/td>\n<td>Removes all elements from the list <code>lst<\/code>&#8211;which becomes empty.<\/td>\n<\/tr>\n<tr>\n<td><code>lst.copy()<\/code><\/td>\n<td>Returns a copy of the list <code>lst<\/code>. Copies only the list, not the elements in the list (shallow copy).<\/td>\n<\/tr>\n<tr>\n<td><code>lst.count(x)<\/code><\/td>\n<td>Counts the number of occurrences of element <code>x<\/code> in the list <code>lst<\/code>.<\/td>\n<\/tr>\n<tr>\n<td><code>lst.extend(iter)<\/code><\/td>\n<td>Adds all elements of an iterable <code>iter<\/code>(e.g. another list) to the list <code>lst<\/code>. <\/td>\n<\/tr>\n<tr>\n<td><code>lst.index(x)<\/code><\/td>\n<td>Returns the position (index) of the first occurrence of value <code>x<\/code> in the list <code>lst<\/code>. <\/td>\n<\/tr>\n<tr>\n<td><code>lst.insert(i, x)<\/code><\/td>\n<td>Inserts element <code>x<\/code> at position (index) <code>i<\/code> in the list <code>lst<\/code>.<\/td>\n<\/tr>\n<tr>\n<td><code>lst.pop()<\/code><\/td>\n<td>Removes and returns the final element of the list <code>lst<\/code>. <\/td>\n<\/tr>\n<tr>\n<td><code>lst.remove(x)<\/code><\/td>\n<td>Removes and returns the first occurrence of element <code>x<\/code> in the list <code>lst<\/code>. <\/td>\n<\/tr>\n<tr>\n<td><code>lst.reverse()<\/code><\/td>\n<td>Reverses the order of elements in the list <code>lst<\/code>. <\/td>\n<\/tr>\n<tr>\n<td><code>lst.sort()<\/code><\/td>\n<td>Sorts the elements in the list <code>lst<\/code> in ascending order.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<p>If you&#8217;ve studied the table carefully, you&#8217;ll know the most important list methods in Python. Let&#8217;s have a look at some examples of above methods:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">>>> l = []\n>>> l.append(2)\n>>> l\n[2]\n>>> l.clear()\n>>> l\n[]\n>>> l.append(2)\n>>> l\n[2]\n>>> l.copy()\n[2]\n>>> l.count(2)\n1\n>>> l.extend([2,3,4])\n>>> l\n[2, 2, 3, 4]\n>>> l.index(3)\n2\n>>> l.insert(2, 99)\n>>> l\n[2, 2, 99, 3, 4]\n>>> l.pop()\n4\n>>> l.remove(2)\n>>> l\n[2, 99, 3]\n>>> l.reverse()\n>>> l\n[3, 99, 2]\n>>> l.sort()\n>>> l\n[2, 3, 99]<\/pre>\n<h2>Action Steps:<\/h2>\n<ul>\n<li>Master <a rel=\"noreferrer noopener\" aria-label=\"Python Sets (opens in a new tab)\" href=\"https:\/\/blog.finxter.com\/sets-in-python\/\" target=\"_blank\">Python Sets<\/a> (with Harry Potter Examples)<\/li>\n<li>Master <a rel=\"noreferrer noopener\" aria-label=\"Python Dictionaries (opens in a new tab)\" href=\"https:\/\/blog.finxter.com\/python-dictionary\/\" target=\"_blank\">Python Dictionaries<\/a> (the ultimate blog tutorial)<\/li>\n<li>Join my <a rel=\"noreferrer noopener\" aria-label=\"free email computer science academy (opens in a new tab)\" href=\"https:\/\/blog.finxter.com\/subscribe\/\" target=\"_blank\">free email computer science academy<\/a> for continuous improvement in Python<\/li>\n<\/ul>\n<p>Thanks for taking the time to reading this article! <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/72x72\/1f40d.png\" alt=\"\ud83d\udc0d\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The most important collection data type in Python is the list data type. You&#8217;ll use lists basically in all your future projects so take 3-5 minutes and study this short guide carefully. You can also play my short video tutorial as you read over the methods: Method Description lst.append(x) Appends element x to the list [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[857],"tags":[73,468,528],"class_list":["post-109986","post","type-post","status-publish","format-standard","hentry","category-python-tut","tag-programming","tag-python","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/109986","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/comments?post=109986"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/109986\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=109986"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=109986"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=109986"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}