{"id":64,"date":"2024-01-25T09:26:48","date_gmt":"2024-01-25T09:26:48","guid":{"rendered":"https:\/\/learnpython.elegantwallp.com\/?p=64"},"modified":"2024-01-25T09:26:51","modified_gmt":"2024-01-25T09:26:51","slug":"python-list","status":"publish","type":"post","link":"https:\/\/learnpython.elegantwallp.com\/2024\/01\/25\/python-list\/","title":{"rendered":"Python List"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you\u2019ll learn about Python List type and how to manipulate list elements effectively.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is a List<\/h2>\n\n\n\n<p>A list is an ordered collection of items.<\/p>\n\n\n\n<p>Python uses the square brackets (<code>[]<\/code>) to indicate a list. The following shows an empty list:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>empty_list = &#91;]<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Typically, a list contains one or more items. To separate two items, you use a comma (,). For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>todo_list = &#91;'Learn Python List','How to manage List elements']<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n\n\n<p>Since a list often contains many items, it\u2019s a good practice to name it using plural nouns e.g.,&nbsp;<code>numbers<\/code>,&nbsp;<code>colors<\/code>, and&nbsp;<code>shopping_carts<\/code>.<\/p>\n\n\n\n<p>The following example defines a list of six\u00a0numbers:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>numbers = &#91;1, 3, 2, 7, 9, 4]<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>If you print out the list, you\u2019ll see its representation including the square brackets. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>print(numbers)<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>&#91;1, 3, 2, 7, 9, 4]<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n\n\n<p>The following shows how to define a list of\u00a0strings:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>colors = &#91;'red', 'green', 'blue'] print(colors)<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>&#91;'red', 'green', 'blue']<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n\n\n<p>A list can contain other lists. The following example defines a list of lists:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>coordinates = &#91;&#91;0, 0], &#91;100, 100], &#91;200, 200]] print(coordinates)<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n\n\n<pre class=\"wp-block-code\"><code><code>&#91;&#91;0, 0], &#91;100, 100], &#91;200, 200]]<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Accessing elements in a list<\/h2>\n\n\n\n<p>Since a list is an ordered collection, you can access its elements by indexes like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>list&#91;index]<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Lists are zero-based indexes. In other words, the first element has an index of 0, the second element has an index of 1, and so on.<\/p>\n\n\n\n<p>For example, the following shows how to access the first element of the\u00a0<code>numbers<\/code>\u00a0list:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>numbers = &#91;1, 3, 2, 7, 9, 4] print(numbers&#91;0])<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>1<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>The\u00a0<code>numbers[1]<\/code>\u00a0will return the second element from the list:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>numbers = &#91;1, 3, 2, 7, 9, 4] print(numbers&#91;1])<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>3<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>The negative index allows you to access elements starting from the end of the list.<\/p>\n\n\n\n<p>The\u00a0<code>list[-1]<\/code>\u00a0returns the last element. The\u00a0<code>list[-2]<\/code>\u00a0returns the second last element, and so on. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>numbers = &#91;1, 3, 2, 7, 9, 4] print(numbers&#91;-1]) print(numbers&#91;-2])<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>4 9<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Modifying, adding, and removing elements<\/h2>\n\n\n\n<p>A list is dynamic. It means that you can modify elements in the list, add new elements to the list, and remove elements from a list.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1) Modifying elements in a list<\/h3>\n\n\n\n<p>To change an element, you assign a new value to it using this syntax:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>list&#91;index] = new_value<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>The following example shows how to change the first element of the\u00a0<code>numbers<\/code>\u00a0list to 10:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>numbers = &#91;1, 3, 2, 7, 9, 4] numbers&#91;0] = 10 print(numbers)<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>&#91;10, 3, 2, 7, 9, 4]<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>The following shows how to multiply the second element by 10:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>numbers = &#91;1, 3, 2, 7, 9, 4] numbers&#91;1] = numbers&#91;1]*10 print(numbers) <\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>&#91;1, 30, 2, 7, 9, 4]<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>And the following divides the third element by 2:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>numbers = &#91;1, 3, 2, 7, 9, 4] numbers&#91;2] \/= 2 print(numbers)<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>&#91;1, 3, 1.0, 7, 9, 4]<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">2) Adding elements to the list<\/h3>\n\n\n\n<p>The\u00a0<code>append()<\/code>\u00a0method appends an element to the end of a list. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>numbers = &#91;1, 3, 2, 7, 9, 4] numbers.append(100) print(numbers)<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>&#91;1, 3, 2, 7, 9, 4, 100]<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>The&nbsp;<code>insert()<\/code>&nbsp;method adds a new element at any position in the list.<\/p>\n\n\n\n<p>For example, the following inserts the number 100 at index 2 of the\u00a0<code>numbers<\/code>\u00a0list:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>numbers = &#91;1, 3, 2, 7, 9, 4] numbers.insert(2, 100) print(numbers)<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>&#91;1, 3, 100, 2, 7, 9, 4]<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3) Removing elements from a list<\/h3>\n\n\n\n<p>The&nbsp;<code>del<\/code>&nbsp;statement allows you to remove an element from a list by specifying the position of the element.<\/p>\n\n\n\n<p>The following example shows how to remove the first element from the list:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>numbers = &#91;1, 3, 2, 7, 9, 4] del numbers&#91;0] print(numbers)<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>&#91;3, 2, 7, 9, 4]<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>The\u00a0<code>pop()<\/code>\u00a0method removes the last element from a list and returns that element:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>numbers = &#91;1, 3, 2, 7, 9, 4] last = numbers.pop() print(last) print(numbers) <\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>4 &#91;1, 3, 2, 7, 9]<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Typically, you use the&nbsp;<code>pop()<\/code>&nbsp;method when you want to remove an element from a list and still want to access the value of that element.<\/p>\n\n\n\n<p>To pop an element by its position, you use the\u00a0<code>pop()<\/code>\u00a0with the element\u2019s index. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>numbers = &#91;1, 3, 2, 7, 9, 4] second = numbers.pop(1) print(second) print(numbers)<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>3 &#91;1, 2, 7, 9, 4]<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>To remove an element by value, you use the&nbsp;<code>remove()<\/code>&nbsp;method. Note that the&nbsp;<code>remove()<\/code>&nbsp;method removes only the first element it encounters in the list.<\/p>\n\n\n\n<p>For example, the following removes the element with value 9 from the\u00a0<code>numbers<\/code>\u00a0list:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>numbers = &#91;1, 3, 2, 7, 9, 4, 9] numbers.remove(9) print(numbers)<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>&#91;1, 3, 2, 7, 4, 9]<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>In this example, the&nbsp;<code>remove()<\/code>&nbsp;method removes only the first number 9 but it doesn\u2019t remove the second number 9 from the list.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: in this tutorial, you\u2019ll learn about Python List type and how to manipulate list elements effectively. What is a List A list is an ordered collection of items. Python uses the square brackets ([]) to indicate a list. The following shows an empty list: Typically, a list contains one or more items. To separate [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[],"class_list":["post-64","post","type-post","status-publish","format-standard","hentry","category-5-lists"],"_links":{"self":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/64","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/comments?post=64"}],"version-history":[{"count":1,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/64\/revisions"}],"predecessor-version":[{"id":65,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/64\/revisions\/65"}],"wp:attachment":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/media?parent=64"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/categories?post=64"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/tags?post=64"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}