{"id":4413,"date":"2022-06-01T19:27:00","date_gmt":"2022-06-02T00:27:00","guid":{"rendered":"https:\/\/wanderin.dev\/?p=4413"},"modified":"2024-10-06T10:10:07","modified_gmt":"2024-10-06T15:10:07","slug":"python-lists","status":"publish","type":"post","link":"https:\/\/javierfeliu.me\/python-interview\/python-lists\/","title":{"rendered":"Python Lists"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\" id=\"h-about-this-article\">About this article<\/h2>\n\n\n\n<p>In this article, I&#8217;ll explore <strong>lists<\/strong>, one of 4 built-in types used to store collections in Python.<\/p>\n\n\n\n<p>This post is the first article in a miniseries exploring the topic of built-in collection types in Python. I based the series on my notes while studying for a Python technical interview.<\/p>\n\n\n\n<p>For quick access, here is the list of posts on this series:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Python Lists (this article)<\/li><li><a href=\"https:\/\/javierfeliu.me\/python-interview\/python-tuples\/\">Python Tuples<\/a><\/li><li><a href=\"https:\/\/javierfeliu.me\/python-interview\/python-sets\/\">Python Sets<\/a><\/li><li><a href=\"https:\/\/javierfeliu.me\/python-interview\/python-dictionaries\/\">Python Dictionaries<\/a><\/li><\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction to Lists<\/h2>\n\n\n\n<p>In Python, a list is a data type used for storing a collection of objects. Lists have the following characteristics:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Lists have an order<\/h3>\n\n\n\n<p>Lists preserve the order of their elements. Two lists with the same elements but in different order are not equal:<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/wanderindev\/ecbb2eee96a13e77ae5281cbd7b96a13.js\"><\/script>\n\n\n\n<h3 class=\"wp-block-heading\">Lists can contain any arbitrary object<\/h3>\n\n\n\n<p>List elements can be of any data type. The example below shows a list containing integers, floats, boolean, strings, dictionaries, and other lists:<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/wanderindev\/581131bc5af4d9b485c664f64971188f.js\"><\/script>\n\n\n\n<h3 class=\"wp-block-heading\">Lists can contain duplicates<\/h3>\n\n\n\n<p>The same element can exist at different indexes in a list. For example, the list below contains the integer 1 at positions 0 and 2:<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/wanderindev\/6af1d45ab3104c5088daa588c1a9b2fa.js\"><\/script>\n\n\n\n<h3 class=\"wp-block-heading\">Lists are mutable<\/h3>\n\n\n\n<p>After creating a list, you can change it at will. Lists come with many built-in methods that allow you to modify them.  The example below uses the .append() method to add an element to the list:<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/wanderindev\/abde06a91593c0487d1a7e72fcae299b.js\"><\/script>\n\n\n\n<h3 class=\"wp-block-heading\">Lists are dynamic<\/h3>\n\n\n\n<p>Lists grow and shrink as needed when adding and removing elements:<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/wanderindev\/3c18c6c9edb2ec38abcf2e2666f3ca02.js\"><\/script>\n\n\n\n<h2 class=\"wp-block-heading\">Creating a list<\/h2>\n\n\n\n<p>You can create a list using a list literal by wrapping its elements in square brackets:<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/wanderindev\/7b6cf2a9d80b17275462b4cf7e3801b6.js\"><\/script>\n\n\n\n<p>You can also use the list() constructor:<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/wanderindev\/a25f6f821b4388de404b3bf69979bb5d.js\"><\/script>\n\n\n\n<h2 class=\"wp-block-heading\">Accessing elements from a list<\/h2>\n\n\n\n<p>You can access an element from a list by using the element index. The index indicates the position of the element within the list. Indexes in sequential data types in Python start at 0.<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/wanderindev\/bca270a058c5a38f84832f4198e5866c.js\"><\/script>\n\n\n\n<h2 class=\"wp-block-heading\">List slicing<\/h2>\n\n\n\n<p>By slicing a list, you can obtain a new list that is a sub-list of the original. The syntax for slicing is:&nbsp;&nbsp;<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/wanderindev\/2beb5987ce65678096e6cafc2ee218fd.js\"><\/script>\n\n\n\n<p>Which would return a sub-list from index = start to index = (end -1) using the given step (or 1 if no step is given).<\/p>\n\n\n\n<p>Let&#8217;s look a some examples.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-slicing-with-implied-step\">Slicing with implied step<\/h3>\n\n\n\n<script src=\"https:\/\/gist.github.com\/wanderindev\/c7bb2d7362648f8210466cf35368451b.js\"><\/script>\n\n\n\n<h3 class=\"wp-block-heading\">Slicing with specified step<\/h3>\n\n\n\n<script src=\"https:\/\/gist.github.com\/wanderindev\/884ff731a07eb254268330f47183448e.js\"><\/script>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-slicing-with-negative-step\">Slicing with negative step<\/h3>\n\n\n\n<script src=\"https:\/\/gist.github.com\/wanderindev\/c893290084a5c586095c6e1b3bcf2fd5.js\"><\/script>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-slicing-from-the-beginning-of-the-list\">Slicing from the beginning of the list<\/h3>\n\n\n\n<script src=\"https:\/\/gist.github.com\/wanderindev\/e149040c58ff85abcfb3774ca80d7bb6.js\"><\/script>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-slicing-to-the-end-of-the-list\">Slicing to the end of the list<\/h3>\n\n\n\n<script src=\"https:\/\/gist.github.com\/wanderindev\/ac079e6442a0205c55972faa6d29e9a8.js\"><\/script>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-slicing-with-negative-indexes\">Slicing with negative indexes<\/h3>\n\n\n\n<script src=\"https:\/\/gist.github.com\/wanderindev\/17675b107cf59dfd9ad00b07938c2108.js\"><\/script>\n\n\n\n<h2 class=\"wp-block-heading\">List built-in methods<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">list.append(x)<\/h3>\n\n\n\n<p>The .append(x) method adds an item to the end of the list. This method has a constant time complexity of O(1).<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/wanderindev\/bea1c3c0f3c0f8ce2362a84d9ef50f0f.js\"><\/script>\n\n\n\n<h3 class=\"wp-block-heading\">list.extend(iterable)<\/h3>\n\n\n\n<p>The .extend(iterable) method appends all elements in the iterable to the original list. This method has a linear time complexity of O(k), where k is the length of the iterable.<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/wanderindev\/3e9370840fd77fbe94e7fdc59ffe6d31.js\"><\/script>\n\n\n\n<h3 class=\"wp-block-heading\">list.insert(i, x)<\/h3>\n\n\n\n<p>The .insert(i, x) method inserts item x in position i of the list. This method has a linear time complexity of O(n), where n is the length of the list.<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/wanderindev\/135df6e712535fe3b7a1cff516a29ec3.js\"><\/script>\n\n\n\n<h3 class=\"wp-block-heading\">list.remove(x)<\/h3>\n\n\n\n<p>The .remove(x) method removes the first element with the value x from the list. It raises a ValueError if x is not in the list. This method has a linear time complexity of O(n), where n is the length of the list.<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/wanderindev\/c027eca6e8746c64198eb3a8040c63c1.js\"><\/script>\n\n\n\n<h3 class=\"wp-block-heading\">list.pop(i)<\/h3>\n\n\n\n<p>The .pop(i) method returns the element in position i and removes it from the list. If you don&#8217;t pass i to the method, it removes the last element in the list. This method has a constant time complexity O(1) when popping from the end and a linear time complexity O(n) when popping from i, where n is the length of the list.<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/wanderindev\/8a93470b2c90bc7c9b89b0ca77d07d27.js\"><\/script>\n\n\n\n<h3 class=\"wp-block-heading\">list.clear()<\/h3>\n\n\n\n<p>The .clear() method removes all elements from the list. This method has a linear time complexity of O(n), where n is the length of the list.<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/wanderindev\/ebae1c24e1161e7902c4fba59bfa6691.js\"><\/script>\n\n\n\n<h3 class=\"wp-block-heading\">list.index(x)<\/h3>\n\n\n\n<p>The .index(x) method returns the index of the first occurrence of x in the list. This method has a linear time complexity of O(n), where n is the length of the list.<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/wanderindev\/9eb501a3348c977a56a43186179c409c.js\"><\/script>\n\n\n\n<h3 class=\"wp-block-heading\">list.count(x)<\/h3>\n\n\n\n<p>The .count(x) method returns the number of times x appears in the list. This method has a linear time complexity of O(n), where n is the length of the list.<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/wanderindev\/9eb1b0b3d6f63ef977a950a9d2c8a830.js\"><\/script>\n\n\n\n<h3 class=\"wp-block-heading\">list.sort()<\/h3>\n\n\n\n<p>The .sort() method sorts the list in place. This method has a linear-logarithmic time complexity of O(n log n), where n is the length of the list.<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/wanderindev\/7dec3bbe444faf0d1489c72c1bc55401.js\"><\/script>\n\n\n\n<h3 class=\"wp-block-heading\">list.reverse()<\/h3>\n\n\n\n<p>The .reverse() method reverses the order of the list in place. This method has a linear time complexity of O(n), where n is the length of the list.<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/wanderindev\/357da519ee401c26f81239103317115e.js\"><\/script>\n\n\n\n<h3 class=\"wp-block-heading\">list.copy()<\/h3>\n\n\n\n<p>The .copy() method returns a shallow copy of the list. This method has a linear time complexity of O(n), where n is the length of the list.<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/wanderindev\/605802836aef6a6f34f8260ebac464bd.js\"><\/script>\n\n\n\n<h2 class=\"wp-block-heading\">List comprehensions<\/h2>\n\n\n\n<p>List comprehensions provide a concise way to create new lists by applying some operation or filter to another sequence or iterable. Let&#8217;s look at a few examples.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Creating a list of squares<\/h3>\n\n\n\n<p>Say we need a list of the squares of the integers in another list. We could use an iterative solution such as:<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/wanderindev\/cd22237c8f9d7dfc7cd094985feb4cbd.js\"><\/script>\n\n\n\n<p>Or we could use the following list comprehension, applying the x**2 operation to each element in the original list. The list comprehension is shorter, more readable, and more expressive than the iterative solution:<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/wanderindev\/82b59d2d09f36eee47d01ca3f96c844f.js\"><\/script>\n\n\n\n<h3 class=\"wp-block-heading\">Creating a list of even numbers<\/h3>\n\n\n\n<p>Below is an iterative solution for creating a list of even numbers from another list of numbers:<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/wanderindev\/62f329e8b935bc134c66cefa982d85ff.js\"><\/script>\n\n\n\n<p>We can apply a filter to a list comprehension to achieve the same result in a concise way:<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/wanderindev\/46bf768f9f2b24e4917ec94eabea05de.js\"><\/script>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>In this article, we covered Python lists, a built-in data type you will most likely use in any non-trivial coding project. We covered the main characteristics of lists, the topic of list slicing, the list&#8217;s built-in methods with their time complexity, and briefly touched on list comprehension.<\/p>\n\n\n\n<p>I hope that the information covered will help you take advantage of Python lists in your next programming project.<\/p>\n\n\n\n<p>The <a href=\"https:\/\/javierfeliu.me\/python-interview\/python-tuples\/\">next article<\/a> explores Python tuples.  See you there!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">References<\/h2>\n\n\n\n<p><a href=\"https:\/\/docs.python.org\/3\/tutorial\/datastructures.html\" target=\"_blank\" rel=\"noreferrer noopener\">Python documentation &#8211; Data Structures<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/www.w3schools.com\/python\/python_lists_access.asp\" target=\"_blank\" rel=\"noreferrer noopener\">W3Schools &#8211; Python Lists<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/wiki.python.org\/moin\/TimeComplexity\" target=\"_blank\" rel=\"noreferrer noopener\">Python TimeComplexity<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/realpython.com\/python-lists-tuples\/\" target=\"_blank\" rel=\"noreferrer noopener\">Real Python &#8211; Lists and Tuples in Python<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/www.geeksforgeeks.org\/python-list-slicing\/\" target=\"_blank\" rel=\"noreferrer noopener\">Geeks for Geeks &#8211; Python List Slicin<\/a>g<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This article explores the lists, one of four built-in collection types in Python. First, we review the properties of lists, their methods with their time complexity, the topic of slicing, and list comprehensions.<\/p>\n<p>This article is the first in a miniseries exploring built-in collection types in Python.<\/p>\n","protected":false},"author":152069353,"featured_media":4613,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_coblocks_attr":"","_coblocks_dimensions":"","_coblocks_responsive_height":"","_coblocks_accordion_ie_support":"","footnotes":""},"categories":[4504528,4504527],"tags":[4504533,4504531],"class_list":["post-4413","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-built-in-collections","category-python-interview","tag-built-in-collections","tag-python-interview"],"jetpack_featured_media_url":"https:\/\/javierfeliu.me\/wp-content\/uploads\/2022\/06\/1.png","amp_enabled":true,"_links":{"self":[{"href":"https:\/\/javierfeliu.me\/wp-json\/wp\/v2\/posts\/4413","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/javierfeliu.me\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/javierfeliu.me\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/javierfeliu.me\/wp-json\/wp\/v2\/users\/152069353"}],"replies":[{"embeddable":true,"href":"https:\/\/javierfeliu.me\/wp-json\/wp\/v2\/comments?post=4413"}],"version-history":[{"count":61,"href":"https:\/\/javierfeliu.me\/wp-json\/wp\/v2\/posts\/4413\/revisions"}],"predecessor-version":[{"id":4811,"href":"https:\/\/javierfeliu.me\/wp-json\/wp\/v2\/posts\/4413\/revisions\/4811"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/javierfeliu.me\/wp-json\/wp\/v2\/media\/4613"}],"wp:attachment":[{"href":"https:\/\/javierfeliu.me\/wp-json\/wp\/v2\/media?parent=4413"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/javierfeliu.me\/wp-json\/wp\/v2\/categories?post=4413"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/javierfeliu.me\/wp-json\/wp\/v2\/tags?post=4413"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}