{"id":3283,"date":"2022-11-14T20:16:21","date_gmt":"2022-11-14T20:16:21","guid":{"rendered":"https:\/\/gyanipandit.com\/programming\/?p=3283"},"modified":"2022-11-14T20:16:23","modified_gmt":"2022-11-14T20:16:23","slug":"python-list-index","status":"publish","type":"post","link":"https:\/\/gyanipandit.com\/programming\/python-list-index\/","title":{"rendered":"Python List index() Method"},"content":{"rendered":"<p style=\"text-align: center;\">Python List index() Method<\/p>\n<p>Now, we are going to learn about the index method. Using the index method, we get the first occurrence index of some element in the list. Simply speaking, we would give some element to the index method as an argument, and it is returning the first occurrence index of that element in the list, which means at what position the element has first occurred in the list. Let\u2019s have a look at a simple program, which demonstrates the index method<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-3284 size-full\" src=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-index-METHOD-IN-PYTHON.jpg\" alt=\"\" width=\"997\" height=\"123\" srcset=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-index-METHOD-IN-PYTHON.jpg 997w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-index-METHOD-IN-PYTHON-300x37.jpg 300w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-index-METHOD-IN-PYTHON-768x95.jpg 768w\" sizes=\"auto, (max-width: 997px) 100vw, 997px\" \/><\/p>\n<h2 style=\"text-align: center;\">Python List index() Method<\/h2>\n<p>As you can see in the above program, we have a simple list, and then we are trying to use the index method. We are trying to get the first occurrence of 12 in the list, and the output, as we can guess, comes out to be 0. You can try executing the program and observing the output of the program yourselves.<\/p>\n<p>Now, Let\u2019s say that we provide some element to the index method, which is not there in the list, in such a situation, we get an error, saying that the element is not in the list. Let\u2019s have a look at another program, which demonstrates the same thing.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-3285 size-full\" src=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-index-METHOD-IN-PYTHON-1.jpg\" alt=\"\" width=\"915\" height=\"128\" srcset=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-index-METHOD-IN-PYTHON-1.jpg 915w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-index-METHOD-IN-PYTHON-1-300x42.jpg 300w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-index-METHOD-IN-PYTHON-1-768x107.jpg 768w\" sizes=\"auto, (max-width: 915px) 100vw, 915px\" \/><\/p>\n<p>As you can see in the above program, we have provided an element to the index method as an argument, which is not there in the list, so in such a case, we get an error. You can also execute the above program and observe the output. We can use the index method in our program, as and when required.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python List index() Method Now, we are going to learn about the index method. Using the index method, we get the first occurrence index of some element in the list. Simply speaking, we would give some element to the index method as an argument, and it is returning the first occurrence index of that element [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[26,27],"class_list":{"0":"post-3283","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-python-tutorial","7":"tag-python","8":"tag-python-tutorial"},"_links":{"self":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3283","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/comments?post=3283"}],"version-history":[{"count":2,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3283\/revisions"}],"predecessor-version":[{"id":4081,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3283\/revisions\/4081"}],"wp:attachment":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/media?parent=3283"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/categories?post=3283"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/tags?post=3283"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}