{"id":7938,"date":"2024-04-11T12:02:52","date_gmt":"2024-04-11T12:02:52","guid":{"rendered":"https:\/\/www.skillvertex.com\/blog\/?p=7938"},"modified":"2024-04-11T12:02:52","modified_gmt":"2024-04-11T12:02:52","slug":"python-lists","status":"publish","type":"post","link":"https:\/\/www.skillvertex.com\/blog\/python-lists\/","title":{"rendered":"Python Lists"},"content":{"rendered":"\n<div class=\"wp-block-rank-math-toc-block\" id=\"rank-math-toc\" id=\"rank-math-toc\"><p>Table of Contents<\/p><nav><ul><li ><a href=\"#what-is-a-python-list\">What is a Python List?<\/a><\/li><li ><a href=\"#what-is-for-and-in\">What is FOR and IN?<\/a><\/li><li ><a href=\"#what-is-range\">What is Range?<\/a><\/li><li ><a href=\"#what-is-a-while-loop\">What is a While Loop?<\/a><\/li><li ><a href=\"#what-is-list-methods\">What is List Methods?<\/a><\/li><li ><a href=\"#what-is-list-build-up\">What is List Build-Up<\/a><\/li><li ><a href=\"#what-is-list-slice\">What is List Slice?<\/a><\/li><li ><a href=\"#conclusion\">Conclusion<\/a><\/li><li ><a href=\"#python-list-fa-qs\">Python  List &#8211; FAQs<\/a><\/li><\/ul><\/nav><\/div>\n\n\n\n<p>Python consists of the built-in type which is referred to as List. The List literal will be written within the square brackets. The list will work similarly to the strings. It is required to use the len() function and square brackets to access data with the first element. Let us look into the article to learn more about <a href=\"https:\/\/www.skillvertex.com\/blog\/mern-stack-with-python\/\" data-type=\"post\" data-id=\"4309\">Python <\/a>List.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-a-python-list\">What is a Python List?<\/h2>\n\n\n\n<p>The list refers to the<a href=\"https:\/\/www.skillvertex.com\/blog\/data-structures-interview-questions-and-answers\/\" data-type=\"post\" data-id=\"3670\"> data structure<\/a> in <a href=\"https:\/\/www.skillvertex.com\/blog\/mern-stack-vs-python-full-stack\/\" data-type=\"post\" data-id=\"4241\">Python <\/a>that will be mutable and will be an ordered sequence of <a href=\"https:\/\/www.skillvertex.com\/blog\/which-one-is-not-an-element-of-iot\/\" data-type=\"post\" data-id=\"3089\">elements<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-for-and-in\">What is FOR and IN?<\/h2>\n\n\n\n<p><a href=\"https:\/\/www.skillvertex.com\/blog\/mern-stack-vs-python-full-stack\/\" data-type=\"post\" data-id=\"4241\">Pythons <\/a>for and it will be useful and are used in the <a href=\"https:\/\/www.skillvertex.com\/blog\/mern-stack-vs-python\/\" data-type=\"post\" data-id=\"4258\">Python <\/a>lists. The *for construct &#8212; for <a href=\"https:\/\/www.skillvertex.com\/blog\/variable-length-arguments-for-macros\/\" data-type=\"post\" data-id=\"3327\">var <\/a>in the list will be the easy way to see each element in the list.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Define a list of numbers\nnumbers = &#91;1, 2, 3, 4, 5]\n\n# Use a for loop to iterate over each number in the list\nfor num in numbers:\n    # Calculate the square of each number\n    square = num ** 2\n    \n    # Print the result\n    print(f\"The square of {num} is: {square}\")\n<\/code><\/pre>\n\n\n\n<p>The in-construct will be the simple way to test if the element is visible in the list&#8211; value in the collection&#8211; and will test whether the value in the collection will return True or False.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>The square of 1 is: 1\nThe square of 2 is: 4\nThe square of 3 is: 9\nThe square of 4 is: 16\nThe square of 5 is: 25\n<\/code><\/pre>\n\n\n\n<p>The for\/ in constructs are mostly used in the Python code and will work on data types other than the list. It is important to important to memorize the <a href=\"https:\/\/www.skillvertex.com\/blog\/python-syntax\/\" data-type=\"post\" data-id=\"6924\">syntax<\/a>. It is possible to use the for\/ in to work on the <a href=\"https:\/\/www.skillvertex.com\/blog\/c-string-functions\/\" data-type=\"post\" data-id=\"2675\">string<\/a>. The <a href=\"https:\/\/www.skillvertex.com\/blog\/array-of-strings-in-c\/\" data-type=\"post\" data-id=\"2641\">string <\/a>will take the place of chars and will print all the <a href=\"https:\/\/www.skillvertex.com\/blog\/character-arithmetic-in-c-and-c\/\" data-type=\"post\" data-id=\"1876\">chars <\/a>in the string.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-range\">What is Range?<\/h2>\n\n\n\n<p>In Python, the range() <a href=\"https:\/\/www.skillvertex.com\/blog\/static-functions-in-c\/\" data-type=\"post\" data-id=\"3038\">function <\/a>helps us create a series of numbers that we can use in a loop. There are two common ways to use it:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>range(n): This makes a series of numbers starting from 0 up to (but not including) n.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>for i in range(5):\n    print(i)\n# Output: 0, 1, 2, 3, 4\n<\/code><\/pre>\n\n\n\n<p>2. <strong>range(a,b)<\/strong>: This will make the series that will begin from a and go up to b.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for j in range(2, 6):\n    print(j)\n# Output: 2, 3, 4, 5\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-a-while-loop\">What is a While Loop?<\/h2>\n\n\n\n<p>Python consists of the standard <a href=\"https:\/\/www.skillvertex.com\/blog\/python-while-loop\/\" data-type=\"post\" data-id=\"7236\">while-loop<\/a>, <a href=\"https:\/\/www.skillvertex.com\/blog\/break-statement-in-c\/\" data-type=\"post\" data-id=\"2014\">break<\/a>, and <a href=\"https:\/\/www.skillvertex.com\/blog\/python-continue-statement\/\" data-type=\"post\" data-id=\"7246\">continue statements<\/a> that will operate for<a href=\"https:\/\/www.skillvertex.com\/blog\/macros-and-its-types-in-c-c\/\" data-type=\"post\" data-id=\"3274\"> C++ <\/a>and Java. So, this will alter the course of the innermost loop. The above for\/ in loop will work to solve the common case of iterating over every element in the list. Thus, the while <a href=\"https:\/\/www.skillvertex.com\/blog\/do-while-loop-in-c\/\" data-type=\"post\" data-id=\"2291\">loop <\/a>will provide you control over the index numbers. Check the loop  that is  given below:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>  ## Access every 3rd element in a list\n  i = 0\n  while i &lt; len(a):\n    print(a&#91;i])\n    i = i + 3<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-list-methods\">What is List Methods?<\/h2>\n\n\n\n<p>The list of methods that are commonly used is given below:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>list.append(elem):<\/strong><\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Adds a single element to the end of the list.<\/li>\n\n\n\n<li>Remember: It changes the original list and doesn&#8217;t give you a new one.<\/li>\n<\/ul>\n\n\n\n<p>   2.<strong>list.insert(index, elem):<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Puts an element at a specific spot in the list.<\/li>\n\n\n\n<li>Pushes other elements to make room for the new one.<\/li>\n<\/ul>\n\n\n\n<p>    3. <strong>list.extend(list2):<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Adds all elements from another list (list2) to the end of the original list.<\/li>\n\n\n\n<li>You can also use <code>+<\/code> or <code>+=<\/code> for a similar result.<\/li>\n<\/ul>\n\n\n\n<p>   4. <strong>list.index(elem):<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Finds where a particular element is in the list.<\/li>\n\n\n\n<li>Be careful: It might cause an error if the element isn&#8217;t there. Use <code>in<\/code> to check first.<\/li>\n<\/ul>\n\n\n\n<p>   5.<strong>list.remove(elem):<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Locates and deletes the first occurrence of a specific element in the list.<\/li>\n\n\n\n<li>An error pops up if the element isn&#8217;t found.<\/li>\n<\/ul>\n\n\n\n<p>   6. <strong>list.sort():<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Put the list in order from smallest to largest.<\/li>\n\n\n\n<li>But usually, people prefer to use sorted().<\/li>\n<\/ul>\n\n\n\n<p>  7. <strong>list.reverse():<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Turns the list around, flipping the order of elements.<\/li>\n\n\n\n<li>It changes the original list; no new list is made.<\/li>\n<\/ul>\n\n\n\n<p>  8. <strong>list.pop(index):<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Takes out and gives you the element at a certain position.<\/li>\n\n\n\n<li>If you don&#8217;t say where it takes out and gives you the last one.<\/li>\n\n\n\n<li>Think of it as the opposite of append().<\/li>\n<\/ul>\n\n\n\n<p>Thus, we can analyze the methods on the list object. Whereas, the lens () is referred to as a function that will consider the list as an argument.<\/p>\n\n\n\n<p><strong>Example<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\n\ndef print_list(my_list):\n    \"\"\"\n    Display the elements of a list.\n    \"\"\"\n    print(\"List elements:\")\n    for item in my_list:\n        print(item)\n\n# Define an empty list\nmy_list = &#91;]\n\n# Use the append() method to add elements to the list\nmy_list.append(\"apple\")\nmy_list.append(\"banana\")\nmy_list.append(\"orange\")\n\n# Display the original list\nprint(\"Original List:\")\nprint_list(my_list)\n\n# Use the insert() method to add an element at a specific index\nmy_list.insert(1, \"grape\")\n\n# Display the list after insertion\nprint(\"\\nList after insertion:\")\nprint_list(my_list)\n\n# Use the extend() method to add multiple elements to the list\nmore_fruits = &#91;\"kiwi\", \"melon\"]\nmy_list.extend(more_fruits)\n\n# Display the list after extension\nprint(\"\\nList after extension:\")\nprint_list(my_list)\n\n# Use the remove() method to remove a specific element\nmy_list.remove(\"banana\")\n\n# Display the list after removal\nprint(\"\\nList after removal:\")\nprint_list(my_list)\n\n# Use the pop() method to remove and return an element at a specific index\npopped_item = my_list.pop(2)\n\n# Display the list after popping an element\nprint(f\"\\nList after popping {popped_item}:\")\nprint_list(my_list)\n\n<\/code><\/pre>\n\n\n\n<p>Note: The above methods cannot return the modified list but will modify the original list.<\/p>\n\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Original List:\nList elements:\napple\nbanana\norange\n\nList after insertion:\nList elements:\napple\ngrape\nbanana\norange\n\nList after extension:\nList elements:\napple\ngrape\nbanana\norange\nkiwi\nmelon\n\nList after removal:\nList elements:\napple\ngrape\norange\nkiwi\nmelon\n\nList after popping banana:\nList elements:\napple\ngrape\nkiwi\nmelon\n\n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>&#8216;Print list&#8217;  is a method that will take the list ( &#8220;my list&#8221;) as the parameter and provide each in the list as the output.<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li>An empty list my_list is defined.<\/li>\n\n\n\n<li>The append() method is used to add elements to the list.<\/li>\n\n\n\n<li>The print_list() method is called twice to display the original and modified lists.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-list-build-up\">What is List Build-Up<\/h2>\n\n\n\n<p>In List build-up, we can analyze that the pattern is to begin the list as an empty list, and use append() or extend() afterward to add the elements. Let us look into the code below:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>list = &#91;]          ## Start as the empty list\n  list.append('a')   ## Use append() to add elements\n  list.append('b')<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-list-slice\">What is List Slice?<\/h2>\n\n\n\n<p>In List Slice, the slice will operate on the list and will work similarly to the strings. It will function to alter the sub-parts of the list.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>  list = &#91;'a', 'b', 'c', 'd']\n  print(list&#91;1:-1])   ## &#91;'b', 'c']\n  list&#91;0:2] = 'z'    ## replace &#91;'a', 'b'] with &#91;'z']\n  print(list)         ## &#91;'z', 'c', 'd']<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"conclusion\">Conclusion<\/h2>\n\n\n\n<p>In conclusion, understanding Python lists is like gaining a valuable skill for organizing and managing collections of data in programming. Imagine a list as a versatile container capable of holding various items, acting as a dynamic tool to keep track of information. Adding elements is simplified with the append() method, while the insert(index, item) method allows precise placement of items within the list. For adding multiple elements swiftly, the extend() method proves handy. <\/p>\n\n\n\n<p> In essence, a Python list is a powerful and flexible tool, and as you delve deeper into programming, you&#8217;ll uncover even more functionalities that make lists an indispensable part of your coding toolkit.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"python-list-fa-qs\">Python  List &#8211; FAQs<\/h2>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1709796048745\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q1.What are lists in Python?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. A list is referred to as the data structure in Python that is a mutable, or changeable, ordered sequence of elements.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1709796054936\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q2.How to create a Python list?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. Python list can be created using the len() function and square brackets [] to access data with the first element at index 0.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1709796061303\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q3. What is a list data method?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. Python list method will create the list from the iterable construct.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>Python consists of the built-in type which is referred to as List. The List literal will be written within the square brackets. The list will work similarly to the strings. It is required to use the len() function and square brackets to access data with the first element. Let us look into the article to &#8230; <a title=\"Python Lists\" class=\"read-more\" href=\"https:\/\/www.skillvertex.com\/blog\/python-lists\/\" aria-label=\"More on Python Lists\">Read more<\/a><\/p>\n","protected":false},"author":4,"featured_media":8027,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[864],"tags":[964,924,866,965],"class_list":["post-7938","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-tutorial","tag-python-list","tag-python-loop","tag-python-tutorial","tag-python-while","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-33"],"_links":{"self":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/7938","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/comments?post=7938"}],"version-history":[{"count":17,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/7938\/revisions"}],"predecessor-version":[{"id":8912,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/7938\/revisions\/8912"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media\/8027"}],"wp:attachment":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media?parent=7938"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/categories?post=7938"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/tags?post=7938"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}