{"id":10643,"date":"2022-02-15T17:51:11","date_gmt":"2022-02-15T12:21:11","guid":{"rendered":"https:\/\/www.h2kinfosys.com\/blog\/?p=10643"},"modified":"2022-02-16T15:09:27","modified_gmt":"2022-02-16T09:39:27","slug":"understanding-python-lists","status":"publish","type":"post","link":"https:\/\/www.h2kinfosys.com\/blog\/understanding-python-lists\/","title":{"rendered":"Understanding Python Lists"},"content":{"rendered":"\n<p>Lists are considered as dynamically sized arrays that will be used which are utilized in another language. Lists don\u2019t need to be homogeneous always, which makes it the most powerful tool in python. A single list consists of data types like integers, strings, and objects. Lists are mutable and can be altered even after their creation.<\/p>\n\n\n\n<p>Lists in python will be ordered and have a definite count. The elements in a list are indexed as per definite sequence and indexing of a list is done with 0 being the first index. Each element in the list has its definite place in the list where it is allowed to duplicate elements in a list with each element which is having its own distinct place and also credibility.<\/p>\n\n\n\n<p><strong>Creating a list<\/strong><\/p>\n\n\n\n<p>The lists in python can be built by just placing sequencing the square brackets[]. Lists have mutable elements. Consider the example<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>Python program to demonstrate<br># Creating a ListList = []<br>print(&#8220;Blank List: &#8220;)<br>print(List)&nbsp;<br># Creating a List of numbers<br>List = [10, 20, 14]<br>print(&#8220;\\nList of numbers: &#8220;)<br>print(List)&nbsp;<br># Creating a List of strings and accessing<br># using index<br>List = [&#8220;Hello&#8221;, &#8220;For&#8221;, &#8220;people&#8221;]<br>print(&#8220;\\nList Items: &#8220;)<br>print(List[0])print(List[2])&nbsp;<br># Creating a Multi-Dimensional List<br># (By Nesting a list inside a List)<br>List = [[&#8216;Hello&#8217;, &#8216;For&#8217;], [&#8216;people&#8217;]]<br>print(&#8220;\\nMulti-Dimensional List: &#8220;)<br>print(List)<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Output:&nbsp;<\/p>\n\n\n\n<p>Blank List:&nbsp;<\/p>\n\n\n\n<p>[]<\/p>\n\n\n\n<p>List of numbers:&nbsp;<\/p>\n\n\n\n<p>[10, 20, 14]<\/p>\n\n\n\n<p>List Items<\/p>\n\n\n\n<p>Hello<\/p>\n\n\n\n<p>people<\/p>\n\n\n\n<p>Multi-Dimensional List:&nbsp;<\/p>\n\n\n\n<p>[[&#8216;Hello&#8217;, &#8216;For&#8217;], [&#8216;people&#8217;]]<\/p>\n\n\n\n<p><strong>Adding the elements to the list<\/strong><\/p>\n\n\n\n<p><strong>By using the append() method<\/strong><\/p>\n\n\n\n<p>The elements that may be added to the list using the built-in append() function. Only one element that time added to the list by using the append method for any addition of multiple elements with append() method, loops will be used, Tuples that can also be included to the list with the use of the append method because the tuples are immutable. Lists that can be added to the existing list with the use of the append() method. For example<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>Python program to demonstrate<br># Addition of elements in a List<br>&nbsp;# Creating a List<br>List = []<br>print(&#8220;Initial blank List: &#8220;)<br>print(List)&nbsp;<br># Addition of Elements in the List<br>List.append(1)<br>List.append(2)<br>List.append(4)<br>print(&#8220;\\nList whereafter Addition of Three elements: &#8220;)<br>print(List)&nbsp;<br># Adding elements to the List<br># using Iterator<br>for i in range(1, 4):&nbsp;&nbsp;&nbsp;&nbsp;<br>    List.append(i)<br>    print(&#8220;\\nList whereafter Addition of elements from 1-3: &#8220;)<br>    print(List)&nbsp;<br># Adding Tuples to the List<br>List.append((6,5))<br>print(&#8220;\\nList whereafter Addition of a Tuple: &#8220;)<br>print(List)&nbsp;<br># Addition of a&nbsp; ListL<br>ist2 = [&#8216;For&#8217;, &#8216;people&#8217;]<br>List.append(List2)<br>print(&#8220;\\nList whereafter Addition of a List: &#8220;)<br>print(List)<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Output:&nbsp;<\/p>\n\n\n\n<p>Initial blank List:&nbsp;<\/p>\n\n\n\n<p>[]<\/p>\n\n\n\n<p>List whereafter Addition of Three elements:&nbsp;<\/p>\n\n\n\n<p>[1, 2, 4]<\/p>\n\n\n\n<p>List whereafter Addition of elements from 1-3:&nbsp;<\/p>\n\n\n\n<p>[1, 2, 4, 1, 2, 3]<\/p>\n\n\n\n<p>List whereafter Addition of a Tuple:&nbsp;<\/p>\n\n\n\n<p>[1, 2, 4, 1, 2, 3, (6,5)]<\/p>\n\n\n\n<p>List whereafter Addition to List:&nbsp;<\/p>\n\n\n\n<p>[1, 2, 4, 1, 2, 3, (6, 5), [&#8216;For&#8217;, \u2018people&#8217;]]<\/p>\n\n\n\n<p><strong>By using the insert() method<\/strong><\/p>\n\n\n\n<p>The append () method does the addition of elements at the last end of the list, for the addition of elements at the required position, the insert() method is used. Append() which takes only one argument, the insert() method requires two arguments(position,value). For example<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>Python program to demonstrate<br># Addition of elements in a List&nbsp;&nbsp;<br># Creating a List<br>List = [1,2,3,4]<br>print(&#8220;Initial List: &#8220;)<br>print(List)&nbsp;<br># Addition of Element at specific Position<br># (By using Insert Method)<br>List.insert(3, 12)<br>List.insert(0, &#8216;people&#8217;)<br>print(&#8220;\\nList whereafter performing Insert Operation: &#8220;)<br>print(List)<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Output:&nbsp;<\/p>\n\n\n\n<p>Initial List:&nbsp;<\/p>\n\n\n\n<p>[1, 2, 3, 4]<\/p>\n\n\n\n<p>List whereafter performing Insert Operation:&nbsp;<\/p>\n\n\n\n<p>[&#8216;people&#8217;, 1, 2, 3, 12, 4]<\/p>\n\n\n\n<p>Using the extend () method<\/p>\n\n\n\n<p>Apart from append() and insert() method there\u2019s one more method we have for the addition of elements, extend(), this method will be used to add the multiple elements at the end.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>For example<br>Python program to demonstrate<br># Addition of elements in a List&nbsp;<br># Creating a List<br>List = [1, 2, 3, 4]<br>print(&#8220;Initial List: &#8220;)<br>print(List)&nbsp;<br># Addition of multiple elements<br># to the List at the end<br># (using Extend Method)<br>List.extend([8, &#8216;people&#8217;, &#8216;Always&#8217;])<br>print(&#8220;\\nList whereafter performing Extend Operation: &#8220;)<br>print(List)<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Output:&nbsp;<\/p>\n\n\n\n<p>Initial List:&nbsp;<\/p>\n\n\n\n<p>[1, 2, 3, 4]<\/p>\n\n\n\n<p>List whereafter performing Extend Operation:&nbsp;<\/p>\n\n\n\n<p>[1, 2, 3, 4, 8, &#8216;people&#8217;, &#8216;Always&#8217;]<\/p>\n\n\n\n<p>&nbsp;<strong>Questions<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>What are Lists in Python how it is used?<\/li><\/ol>\n\n\n\n<p>2. Explain the insert method with an example?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Lists are considered as dynamically sized arrays that will be used which are utilized in another language. Lists don\u2019t need to be homogeneous always, which makes it the most powerful tool in python. A single list consists of data types like integers, strings, and objects. Lists are mutable and can be altered even after their [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":10650,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[342],"tags":[],"class_list":["post-10643","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-tutorials"],"_links":{"self":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/10643","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/comments?post=10643"}],"version-history":[{"count":0,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/10643\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media\/10650"}],"wp:attachment":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media?parent=10643"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/categories?post=10643"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/tags?post=10643"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}