{"id":10112,"date":"2020-11-04T17:26:33","date_gmt":"2020-11-04T17:26:33","guid":{"rendered":"https:\/\/www.askpython.com\/?p=10112"},"modified":"2023-04-04T09:13:55","modified_gmt":"2023-04-04T09:13:55","slug":"indexing-in-python","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/list\/indexing-in-python","title":{"rendered":"Indexing in Python &#8211; A Complete Beginners Guide"},"content":{"rendered":"\n<p>Data structures in Python include lists, tuples, etc. These data structures can have multiple elements in them, each having some different properties but the problem is how to refer to a particular element from the hundreds of elements they contain. Here indexing comes into action. Indexing is a simple but fundamental concept that is important to learn before further processing with Python data structures.<\/p>\n\n\n\n<p>This tutorial will explain everything you need to know about indexing in Python. But first, let&#8217;s take a quick look at iterables.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Prerequisite &#8211; What are Iterables?<\/h2>\n\n\n\n<p>Before we get started with indexing, let&#8217;s understand what <a class=\"rank-math-link\" href=\"https:\/\/www.askpython.com\/python\/built-in-methods\/python-iterator\">iterables<\/a> are and what is their main function. The knowledge of iterables is much needed to go behind indexing. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Iterables in Python<\/h3>\n\n\n\n<p>Iterables are a special type of <a class=\"rank-math-link\" href=\"https:\/\/www.askpython.com\/python\/oops\/python-classes-objects\">objects in Python<\/a> that you can iterate over. Meaning you can traverse through all the different elements or entities contained within the object. It can be easily achieved using the <a class=\"rank-math-link\" href=\"https:\/\/www.askpython.com\/python\/python-for-loop\">for loops<\/a>. <\/p>\n\n\n\n<p>Under the hood, what all these iterable items carry are two unique methods called __iter__() or __getitem__() that implement the <strong><em>Sequence Semantics<\/em><\/strong>. <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n#Lists are iterable items in Python. \nrandomItems = &#x5B;4, 6, 2, 56, 23, 623, 453]\n\n#Each individual element inside a list can be accessed using a for loop\nfor item in randomItems: \n    print(item)\n<\/pre><\/div>\n\n\n<p><\/p>\n\n\n\n<p><strong>Besides lists in Python, strings and tuples are also iterable. <\/strong><\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ntitle = &quot;Lose Yourself&quot; \n\n#Looping through each character in the string\nfor char in title: \n    print(char)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nL\no\ns\ne\n\nY\no\nu\nr\ns\ne\nl\nf\n<\/pre><\/div>\n\n\n<p>Now that we know what Iterables are in Python. How is this related to indexing?<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is Indexing in Python?<\/h2>\n\n\n\n<p>Indexing in Python is a way to refer to the individual items within an iterable by their position. In other words, you can directly access your elements of choice within an iterable and do various operations depending on your needs. <\/p>\n\n\n\n<p><strong>Before we get into examples of Indexing in Python, there&#8217;s an important thing to Note:<\/strong><\/p>\n\n\n\n<p>In Python, objects are &#8220;zero-indexed&#8221; meaning the position count starts at zero. Many other programming languages follow the same pattern. So, if there are 5 elements present within a list. Then the first element (i.e. the leftmost element) holds the &#8220;zeroth&#8221; position, followed by the elements in the first, second, third, and fourth positions.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Python Index Method<\/h3>\n\n\n\n<p>The index of a specific item within a list can be revealed when the <strong>index()<\/strong> method is called on the list with the item name passed as an argument. <\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\niterable.index(item)\n<\/pre><\/div>\n\n\n<p>Where item is an element that index value we want to get.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nfruits = &#x5B;&quot;apple&quot;, &quot;grape&quot;, &quot;orange&quot;, &quot;guava&quot;, &quot;banana&quot;]\n\n#Printing out the indexes of Apples and Banana\nprint(&quot;Index of Apple: &quot;, fruits.index(&quot;apple&quot;))\nprint(&quot;Index of Banana: &quot;, fruits.index(&quot;banana&quot;))\n<\/pre><\/div>\n\n\n<p><strong>Output: <\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nIndex of Apple: 0\nIndex of Banana: 4\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">Python Index Operator<\/h3>\n\n\n\n<p>The Python Index Operator is represented by opening and closing square brackets: []. The syntax, however, requires you to put a number inside the brackets. <\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\niterable&#x5B;n] \n<\/pre><\/div>\n\n\n<p>Where n is just an integer number representing the position of the element we want to access.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ngreetings = &quot;Hello, World!&quot;\n\nprint(greetings&#x5B;0]) #Prints the 0-th element in our string\n\nprint(greetings&#x5B;5]) #Prints the 5-th element in our string\n\nprint(greetings&#x5B;12]) #Prints the 12-th element in our string\n\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nH\n,\n!\n<\/pre><\/div>\n\n\n<p>We can see how our print function accesses different elements within our string object to get the specific characters we want. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Negative Indexing in Python<\/h3>\n\n\n\n<p>We&#8217;ve recently learned how to use indexing in Lists and Strings to get the specific items of our interest. Although in all our previous cases we&#8217;ve used a positive integer inside our index operator (the square brackets), it&#8217;s not necessarily needed to be that way. <\/p>\n\n\n\n<p>Often, if we are interested in the last few elements of a list or maybe we just want to index the list from the opposite end, we can use negative integers. This process of indexing from the opposite end is called Negative Indexing. <\/p>\n\n\n\n<p><strong>Note: In negative Indexing, the last element is represented by -1 and not -0. <\/strong><\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nletters = &#x5B;&#039;a&#039;, &#039;s&#039;, &#039;d&#039;, &#039;f&#039;]\n\n#We want to print the last element of the list\nprint(letters&#x5B;-1]) #Notice we didn&#039;t use -0 \n\n#To print the 2nd last element from an iterable\nprint(letters&#x5B;-2])\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nf\nd\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>In this tutorial, we&#8217;ve learned that indexing is just a way of referencing the elements of an iterable. We have used the Python index() method to get the index of a particular element. We&#8217;ve also looked at the Python index operator and what negative indexing is. Hope you enjoyed the tutorial and learned how to implement indexing in your next project.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Reference<\/h2>\n\n\n\n<p><a href=\"https:\/\/docs.python.org\/3\/tutorial\/datastructures.html\" target=\"_blank\" rel=\"noopener\">https:\/\/docs.python.org\/3\/tutorial\/datastructures.html<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Data structures in Python include lists, tuples, etc. These data structures can have multiple elements in them, each having some different properties but the problem is how to refer to a particular element from the hundreds of elements they contain. Here indexing comes into action. Indexing is a simple but fundamental concept that is important [&hellip;]<\/p>\n","protected":false},"author":17,"featured_media":10124,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10112","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-list"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/10112","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/17"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=10112"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/10112\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/10124"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=10112"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=10112"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=10112"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}