{"id":18108,"date":"2021-10-25T00:04:24","date_gmt":"2021-10-24T18:34:24","guid":{"rendered":"https:\/\/java2blog.com\/?p=18108"},"modified":"2023-10-10T10:35:36","modified_gmt":"2023-10-10T05:05:36","slug":"initialize-list-with-zeros-in-python","status":"publish","type":"post","link":"https:\/\/java2blog.com\/initialize-list-with-zeros-in-python\/","title":{"rendered":"Four Ways To Initialize List With Zeros In Python"},"content":{"rendered":"<div id=\"toc_container\" class=\"toc_light_blue no_bullets\"><p class=\"toc_title\">Table of Contents<\/p><ul class=\"toc_list\"><li><a href=\"#1_Why_Initialize_List_with_Zeros_in_Python\">1. Why Initialize List with Zeros in Python?<\/a><\/li><li><a href=\"#2_How_Can_We_Initialize_List_With_Zeros_In_Python\">2. How Can We Initialize List With Zeros In Python?<\/a><ul><li><a href=\"#21_Using_Operator\">2.1 Using * Operator<\/a><\/li><li><a href=\"#22_Using_itertoolsrepeat_Function\">2.2 Using itertools.repeat() Function<\/a><\/li><li><a href=\"#23_Using_Generator_Comprehension\">2.3 Using Generator Comprehension<\/a><\/li><li><a href=\"#24_Using_List_Comprehension\">2.4 Using List Comprehension<\/a><\/li><\/ul><\/li><li><a href=\"#3_Conclusion\">3. Conclusion<\/a><\/li><\/ul><\/div>\n\n<p><a href=\"https:\/\/java2blog.com\/python-list-tutorial\/\" target=\"_blank\" data-type=\"URL\" data-id=\"https:\/\/java2blog.com\/python-list-tutorial\/\" rel=\"noreferrer noopener\">Lists <\/a>in Python are one of the most used data structures.\u00a0 In this article, we will first discuss why we should initialize a list with zeros in Python. This will give us an insight into how different ways of creating lists in python can lead to different usage of memory. After that, we will discuss four ways to create list of zeros in python.<\/p>\n\n\n<h2 class=\"wp-block-heading\"><span id=\"1_Why_Initialize_List_with_Zeros_in_Python\"><strong>1. Why Initialize List with Zeros in Python?<\/strong><\/span><\/h2>\n\n\n<p>Generally, we don\u2019t need to initialize lists in Python. We can always <a href=\"https:\/\/java2blog.com\/create-empty-list-python\/\" target=\"_blank\" data-type=\"URL\" data-id=\"https:\/\/java2blog.com\/create-empty-list-python\/\" rel=\"noreferrer noopener\">declare an empty list<\/a> and append elements to it later. But this method comes with a drawback.\u00a0\u00a0<\/p>\n\n\n<p>In python, lists are implemented in such a way that their size grows automatically. At any instance, the size of the list is almost 1.5 times the number of elements actually present in it. This leads to inefficiency in the program as a large part of allocated memory remains unused. You can understand this using the following program.<\/p>\n\n\n<pre class=\"wp-block-code\"><code>list1 = list()\nlist1.append(1)\nlist1.append(2)\nlist1.append(3)\nlist1.append(4)\nlist1.append(5)\nlist2 = [1, 2, 3, 4, 5]\nprint(\"list1 is:\", list1)\nprint(\"Size of list1 is:\", list1.__sizeof__())\nprint(\"list2 is:\", list2)\nprint(\"Size of list2 is:\", list2.__sizeof__())<\/code><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\"><code>list1 is: [1, 2, 3, 4, 5]\nSize of list1 is: 104\nlist2 is: [1, 2, 3, 4, 5]\nSize of list2 is: 80<\/code><\/pre>\n\n\n<p>Here, we have created two lists namely <code>list1<\/code> and <code>list2<\/code> with the same number of elements. The difference lies in how we have created those lists. We have created <code>list1<\/code> by declaring an empty list. After that, we have appended five elements to the list. On the contrary, we have created <code>list2<\/code> by initializing it directly using all the elements.\u00a0<\/p>\n\n\n<p>After that, we have used the <code>__sizeof__()<\/code> method to get the actual size of the list object (not the length). You can easily observe that the memory used by <code>list2<\/code> is significantly less than the memory used by <code>list1<\/code>.\u00a0<\/p>\n\n\n<p>In situations where we need to store thousands of elements in a list, a significant amount of memory will be wasted if we use the <code>append()<\/code> method. So, I would suggest you initialize a list whenever you know the exact number of elements in the list. Let us now discuss how we can initialize a list with zeros in python.<\/p>\n\n\n<h2 class=\"wp-block-heading\"><span id=\"2_How_Can_We_Initialize_List_With_Zeros_In_Python\"><strong>2. How Can We Initialize List With Zeros In Python?<\/strong><\/span><\/h2>\n\n\n<p>We can initialize list with zeros in python using the following four ways.<\/p>\n\n\n<ul>\n<li>By using * operator<\/li>\n\n\n\n<li>By using itertools.repeat() function<\/li>\n\n\n\n<li>By using generator comprehension<\/li>\n\n\n\n<li>By using list comprehension\u00a0<\/li>\n<\/ul>\n\n\n<p>Let us now discuss the above ways one by one.<\/p>\n\n\n<h3 class=\"wp-block-heading\"><span id=\"21_Using_Operator\"><strong>2.1 Using * Operator<\/strong><\/span><\/h3>\n\n\n<p>The multiplication operator \u201c*\u201d can be used to create new lists from existing lists in python. When we multiply a given list with a number N using the * operator, it creates a new list by concatenating the existing list N number of times. You can observe this in the following example.<\/p>\n\n\n<pre class=\"wp-block-code\"><code>list1 = [1, 2, 3, 4, 5]\nprint(\"list1 is:\", list1)\nlist2 = list1 * 3\nprint(\"list2 is:\", list2)<\/code><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\"><code>list1 is: [1, 2, 3, 4, 5]\nlist2 is: [1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5]<\/code><\/pre>\n\n\n<p>To initialize a list with zeros using the <code>* operator<\/code>, we will create a list with 0 as its only element. After that, we will multiply the list by the number of elements required in the output list.\u00a0<\/p>\n\n\n<p>For instance, we can initialize a list with zeros having 10 elements as follows.<\/p>\n\n\n<pre class=\"wp-block-code\"><code>zeroList = [0]*10\nprint(\"The list is:\", zeroList)<\/code><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\"><code>The list is: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]<\/code><\/pre>\n\n\n<h3 class=\"wp-block-heading\"><span id=\"22_Using_itertoolsrepeat_Function\"><strong>2.2 Using itertools.repeat() Function<\/strong><\/span><\/h3>\n\n\n<p>An alternate way to initialize a list with zeros is to use iterators. To initialize a list with zeros, we will create an iterator having 0 as all of its elements. Then, we will create a list from the iterator using the <code>list() <\/code>function.<\/p>\n\n\n<p>To create the iterator, we will use the <code>repeat()<\/code> function defined in the <code>itertools<\/code> module. The syntax for the <code>repeat()<\/code> function is as follows.<\/p>\n\n\n<p><code><strong>itertools<\/strong>.<strong>repeat<\/strong>(<strong>value<\/strong>,<strong>number_of_times_value_is_repeated<\/strong>)<\/code><\/p>\n\n\n<p>The <code>repeat()<\/code> function takes the <strong><code>value<\/code><\/strong> as its first argument and <code>the number of times the value has to be repeated<\/code> as its option argument. In case, we don\u2019t provide the second argument i.e. the number of times the given value has to be repeated, an infinite iterator is created.<\/p>\n\n\n<p>For instance, To create an iterator with 10 zeros, we will have to use the following statement in python.<\/p>\n\n\n<pre class=\"wp-block-code\"><code>myIter= itertools.repeat(0,10)<\/code><\/pre>\n\n\n<p>After creating the iterator, we can use the list() constructor to initialize the list with zeros as follows.<\/p>\n\n\n<pre class=\"wp-block-code\"><code>import itertools\n\nmyIter = itertools.repeat(0, 10)\nzeroList = list(myIter)\nprint(\"The list with zeros is:\", zeroList)<\/code><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\"><code>The list with zeros is: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]<\/code><\/pre>\n\n\n<h3 class=\"wp-block-heading\"><span id=\"23_Using_Generator_Comprehension\"><strong>2.3 Using Generator Comprehension<\/strong><\/span><\/h3>\n\n\n<p>Instead of using iterators, we can also use generators to initialize a list with zeros in python. Here, we will use generator comprehension and the <code>range()<\/code> function to create a generator. The syntax for generator comprehension is as follows.<\/p>\n\n\n<p><code><em>generator<\/em>= <strong>(<\/strong><em>expression<\/em> <strong>for<\/strong> <em>element<\/em> <strong>in<\/strong> <em>iterable<\/em><strong>)<\/strong><\/code><\/p>\n\n\n<p>Here,<\/p>\n\n\n<ul>\n<li>\u201c<code>expression<\/code>\u201d is the value that will be included in the generator. For us, it will be 0 as we have to create a generator with all zeros.<\/li>\n\n\n\n<li>We will create an \u201c<code>iterable<\/code>\u201d with the number of elements required in the list using the <code>range()<\/code> function by passing the number of elements required in the generator as input argument. For instance, to create a generator that yields 10 elements, we will give 10 as the input argument to the <code>range()<\/code> function.<\/li>\n<\/ul>\n\n\n<p>We can create a generator that yields 10 zeros using generator comprehension and the <code>range()<\/code> function as follows.<\/p>\n\n\n<pre class=\"wp-block-code\"><code>myGen = (0 for element in range(10))<\/code><\/pre>\n\n\n<p>After creating the generator, we can initialize the list with zeros using the<code> list()<\/code> constructor as follows.<\/p>\n\n\n<pre class=\"wp-block-code\"><code>myGen = (0 for element in range(10))\nzeroList = list(myGen)\nprint(\"The list with zeros is:\", zeroList)\n<\/code><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\"><code>The list with zeros is: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]\n<\/code><\/pre>\n\n\n<h3 class=\"wp-block-heading\"><span id=\"24_Using_List_Comprehension\"><strong>2.4 Using List Comprehension<\/strong><\/span><\/h3>\n\n\n<p>Instead of using generator comprehension and the <code>list()<\/code> constructor, we can directly use list comprehension to initialize a list with zeros in Python. The syntax for list comprehension is as follows.<\/p>\n\n\n<p><code><em>newList<\/em>= <strong>[<\/strong><em>expression<\/em> <strong>for<\/strong> <em>element<\/em> <strong>in<\/strong> <em>iterable<\/em><strong>]<\/strong><\/code><\/p>\n\n\n<p>Here,\u00a0<\/p>\n\n\n<ul>\n<li>\u201c<code>newList<\/code>\u201d is the newly created list using list comprehension.<\/li>\n\n\n\n<li>\u201c<code>expression<\/code>\u201d is the value that will be included in the <code>newList<\/code>. For us, it will be 0 as we have to create a list with all zeros.<\/li>\n\n\n\n<li>Also, We will create an \u201c<code>iterable<\/code>\u201d with the number of elements required in the list using the <code>range()<\/code> function.\u00a0 We will pass the number of elements required in the list as the input argument to the <code>range()<\/code> function. For instance, to create a list that has 10 elements, we will give 10 as the input argument to the <code>range()<\/code> function.<\/li>\n<\/ul>\n\n\n<p>The program to initialize a list with zeros using list comprehension in python is as follows.<\/p>\n\n\n<pre class=\"wp-block-code\"><code>zeroList = [0 for element in range(10)]\nprint(\"The list with zeros is:\", zeroList)<\/code><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\"><code>The list with zeros is: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]<\/code><\/pre>\n\n\n<h2 class=\"wp-block-heading\"><span id=\"3_Conclusion\">3. Conclusion<\/span><\/h2>\n\n\n<p>In this article, we have discussed four ways to initialize a list with zeros in python. All of the methods have their own benefits and you can use them at your convenience. Whenever you know the number of elements in the list before defining a list, you can initialize a list with zeros and reassign the values in the list. Even if you don\u2019t need a list having zero as its elements, you can do this. This will help you write an efficient program that will require less memory.\u00a0<\/p>\n\n\n<p>I hope you had fun reading this article. Stay tuned for informative articles.<\/p>\n\n\n<p>Happy Learning!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Table of Contents1. Why Initialize List with Zeros in Python?2. How Can We Initialize List With Zeros In Python?2.1 Using * Operator2.2 Using itertools.repeat() Function2.3 Using Generator Comprehension2.4 Using List Comprehension3. Conclusion Lists in Python are one of the most used data structures.\u00a0 In this article, we will first discuss why we should initialize a [&hellip;]<\/p>\n","protected":false},"author":33,"featured_media":18133,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"_mi_skip_tracking":false},"categories":[146],"tags":[],"_links":{"self":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/18108"}],"collection":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/users\/33"}],"replies":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/comments?post=18108"}],"version-history":[{"count":4,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/18108\/revisions"}],"predecessor-version":[{"id":25217,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/18108\/revisions\/25217"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/media\/18133"}],"wp:attachment":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/media?parent=18108"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/categories?post=18108"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/tags?post=18108"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}