{"id":3980,"date":"2020-07-08T16:41:54","date_gmt":"2020-07-08T11:11:54","guid":{"rendered":"https:\/\/www.h2kinfosys.com\/blog\/?p=3980"},"modified":"2020-07-14T16:27:06","modified_gmt":"2020-07-14T10:57:06","slug":"python-list","status":"publish","type":"post","link":"https:\/\/www.h2kinfosys.com\/blog\/python-list\/","title":{"rendered":"Python List:"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">What is List?<\/h2>\n\n\n\n<p>Let\u2019s think of a list as a cargo train that is used to hold different types of goods for a specific time of period. In <a href=\"https:\/\/www.h2kinfosys.com\/courses\/python-online-training\">python<\/a> lists are just like this train, it is used to store items of the same as well as a different data type.<\/p>\n\n\n\n<p><img fetchpriority=\"high\" decoding=\"async\" width=\"581\" height=\"229\" src=\"https:\/\/lh3.googleusercontent.com\/6IWTeazRBHQpNsw0Ut7MlU4YxVUBfeUF73fshzqMtkuxtmI6LS6k47qsX5XueBY9vPAqKgQ972KR1NMIIg1mnxclYCuZNrVyrh2UEMdwGAbMz4VktJD9JWu_TNxi3KCmRN4o3ip-n3TiCG8f6Q\" alt=\"\" title=\"\"><\/p>\n\n\n\n<p>We just stored 5 numbers in a list that can be accessed using num_list variable.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">List_of_num = [1,2,3,4,5]\nprint(List_of_num)<\/pre>\n\n\n\n<p><img decoding=\"async\" width=\"447\" height=\"165\" src=\"https:\/\/lh3.googleusercontent.com\/q0AX-Zy6XPlt5UDR-M55ONcuq_wi9QkmqHsIltI_mcMEozs_5bu8aUVYJxmti332zNfrDD6xjg-VaZVHDr0pDkgQVfXl9mec9kkY6pFoadsAC2ItVoPYaajAHc8E_76f7XE5gsEP6mU0duhwrQ\" alt=\"\" title=\"\"><\/p>\n\n\n\n<p>Different data type elements can also be stored in the list just like the above example. Let\u2019s see<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">list_of_num = ['Hello',5,2.99,'World']\nprint(list_of_num)\n<\/pre>\n\n\n\n<p><img decoding=\"async\" width=\"624\" height=\"168\" src=\"https:\/\/lh4.googleusercontent.com\/5MFLCfXxiaQaKUNQctLLP2qPXYWNri91MXLkLaJTeAe4FutjcgbWrrc4hcYdIIHMkzrLDceuKPE37K7EXLzRVGqmowCGS3iaXROb5ODpso3Flb_Nt-LMfgS-bN-5Tk3jfun9OhWIqL31N3jOwA\" alt=\"\" title=\"\"><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Accessing values within lists<\/h2>\n\n\n\n<p>We can access values stored in the list using the square brackets. Let\u2019s have a look at the example below.<\/p>\n\n\n\n<p>In the list \u201cHello\u201d is at the 0th index, \u20185\u2019 is at the 1st index, \u20182.99\u2019 is at the 2nd index, \u20185\u2019 is at the 4th index,<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">list_of_num = ['Hello',5,2.99,'World']\nprint(list_of_num[0])\nprint(list_of_num[1])\nprint(list_of_num[2])\nprint(list_of_num[3])<\/pre>\n\n\n\n<p>The shown below will be the output.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">List Comprehension<\/h2>\n\n\n\n<p>List comprehensions are used for converting one list into another list or creating a new list from other iterables. We can achieve the same task using a <a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/visual-basic\/programming-guide\/language-features\/control-flow\/loop-structures\" rel=\"nofollow noopener\" target=\"_blank\">simple loop structure<\/a> but list comprehension provides a short syntax to obtain the same results.<\/p>\n\n\n\n<p>Let\u2019s take an example of finding cubes of a list of numbers.&nbsp;<\/p>\n\n\n\n<p>Here is an example of using simple loops.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">numbers = [1, 2, 3, 4, 5]\ncubes = []\nfor n in numbers:\n&nbsp; cubes.append(n**3)\nprint(cubes)<\/pre>\n\n\n\n<p><img loading=\"lazy\" decoding=\"async\" width=\"497\" height=\"295\" src=\"https:\/\/lh5.googleusercontent.com\/aTjz9NSdXuzeRR5jJrQ9bxQjlvrUm0df_KraV_sBqcZRHrcFTL-2RcM2ILQ_sI8RyXRSMjYgI_8AK1dlkmZnctQPXVATwg02ENs7ZuV_5_IqF7hScIlO20X_GO2TcB9VExiPSohfmgPs4jk_9w\" alt=\"\" title=\"\"><\/p>\n\n\n\n<p>Syntax of list comprehension<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">new_list = [expression for_loop_one_or_more conditions]<\/pre>\n\n\n\n<p>Let\u2019s calculate the cubes using list comprehension.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">numbers = [1, 2, 3, 4, 5]\ncubes = [n**3 for n in numbers]\nprint(cubes)<\/pre>\n\n\n\n<p><img loading=\"lazy\" decoding=\"async\" width=\"621\" height=\"230\" src=\"https:\/\/lh4.googleusercontent.com\/iIjC4BJ6Wlg-BT-2onzrpdz9XYGQa8r1MPj5-dTr_knMn3rSeU2MIcuoE7zXEAneC93YDaWgur92BnQts5m0fBa4qEUo6mz3d75r_mYQMNANjbrApsI48NJr9l1HPk1XUg_6Xt8QAlZrmF0KNg\" alt=\"\" title=\"\"><\/p>\n\n\n\n<p>Using list comprehension we achieved the same task using in 3 lines instead of 5 lines.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Append function.<\/h2>\n\n\n\n<p>Append function is used to add an item at the end of python list just like adding another cabin at the end of the train.\u00a0<\/p>\n\n\n\n<p>Let\u2019s take a look at the example.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">numbers = [1, 2, 3, 4, 5]\nnumbers.append(6)\nprint(numbers)<\/pre>\n\n\n\n<p><img loading=\"lazy\" decoding=\"async\" width=\"581\" height=\"267\" src=\"https:\/\/lh5.googleusercontent.com\/5XXwXOh3uP-V51zaAN2rqSluFOYEKxvgzoyR81CjwiGauxODG8mHpWVPJgf-HDOhBSD9pR9A4Qyjei-Oj2437X4rD-2Q_qSqaXe9uyBTkyi2BP2yEW9Fuq4Sv6XpC2-UC5Om8Y3O4qDuUxTSVw\" alt=\"\" title=\"\"><\/p>\n\n\n\n<p>We can append any type of data.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">numbers = [1, 2, 3, 4, 5]\nnumbers.append(\"six\")\nprint(numbers)<\/pre>\n\n\n\n<p><img loading=\"lazy\" decoding=\"async\" width=\"567\" height=\"259\" src=\"https:\/\/lh6.googleusercontent.com\/TALDUI9iDoSmkm2VR1_uBe8trgC4GbqWxTGi3fBEKaTosEIEtuxVpKvyv0E0aF9laoQKK_0mucKj8WZwfK21-6yKurvIwTXgOr42iKID5FMnrSHtSgrM8Issqsd4HifVv-iRHirBmAoY3ojrkw\" alt=\"\" title=\"\"><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Sort function<\/h2>\n\n\n\n<p>The sort function is used to sort the items in the python list in the ascending as well as descending order.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">numbers = [1, 5, 2, 6, 3, 4]\nnumbers.sort()&nbsp; # for ascending order\nprint(numbers)\nnumbers.sort(reverse=True)&nbsp; # for descending order\nprint(numbers)<\/pre>\n\n\n\n<p><img loading=\"lazy\" decoding=\"async\" width=\"624\" height=\"272\" src=\"https:\/\/lh4.googleusercontent.com\/eC_yUijITYmcD-V7yfeZkDsMiL2I5jJJjFWHA5LekusJga7S348cNXWqZz98EssAwP8AvNq41AaElprzeGc1eqvns2Fl0Yi4JkhM38GkklHO-J7nhEjkmpUU5_5FI1Db6GhlBrkVlYCVOkK-tA\" alt=\"\" title=\"\"><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Length function<\/h2>\n\n\n\n<p>Length function is used to find the length of the list. Let\u2019s take a look at an example to find out the length of the list.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">numbers = [1, 5, 2, 6, 3, 4]\nprint(len(numbers))<\/pre>\n\n\n\n<p><img loading=\"lazy\" decoding=\"async\" width=\"624\" height=\"244\" src=\"https:\/\/lh5.googleusercontent.com\/LjPCEt-TSwftmIF13_eUI82g_7vQfMFvtidEMKUSfrHBkKumSEgI_9MALY3Pi5QVYR6uXmi5Xa68eLSC7F0mKDdhnjdSohMrVd5KVDGEJoCJw6tqmrtlVe8ALu4L5AEn7i7qeSaB-Cg2xKTjpg\" alt=\"\" title=\"\"><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Reverse function<\/h2>\n\n\n\n<p>The reverse function is used to reverse the items of the list. Take a look at the example.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">list_of_string = ['I', \"am\", \"here\"]\nlist_of_string.reverse()\nprint(list_of_string)<\/pre>\n\n\n\n<p><img loading=\"lazy\" decoding=\"async\" width=\"629\" height=\"214\" src=\"https:\/\/lh6.googleusercontent.com\/jc3GUyCo4_XN9Z1fsCjz1Q3StxwflgsCYfya4caAgScIBQ0m2uxDo0QutbrolffoDAuzOafL8Wgu82Z1fDCJXr7fvM2IMAcrNXvHxOuoIsUOzYojSLVBN-zkeA0_sfBqVgVQd8v-U_OgPdOYRQ\" alt=\"\" title=\"\"><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>What is List? Let\u2019s think of a list as a cargo train that is used to hold different types of goods for a specific time of period. In python lists are just like this train, it is used to store items of the same as well as a different data type. We just stored 5 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3998,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[342],"tags":[1088,1086,1085,1090,1089,1087],"class_list":["post-3980","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-tutorials","tag-append","tag-comprehension","tag-length","tag-python-list","tag-reverse-examples","tag-sort"],"_links":{"self":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/3980","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=3980"}],"version-history":[{"count":0,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/3980\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media\/3998"}],"wp:attachment":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media?parent=3980"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/categories?post=3980"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/tags?post=3980"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}