{"id":26252,"date":"2022-01-03T09:18:44","date_gmt":"2022-01-03T03:48:44","guid":{"rendered":"https:\/\/python-programs.com\/?p=26252"},"modified":"2022-01-03T09:18:44","modified_gmt":"2022-01-03T03:48:44","slug":"indexing-in-python-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/indexing-in-python-with-examples\/","title":{"rendered":"Indexing in Python with Examples"},"content":{"rendered":"
What exactly is indexing in Python? \u2013 As simple as it appears, explaining how indexing works in Python can be a little tricky. So take a seat and read this article\u00a0to gain a better understanding of indexing in Python.<\/p>\n
Iterables:<\/strong><\/p>\n Before we get started with indexing, let’s define iterables and their primary function. Iterable knowledge is essential for getting\u00a0behind indexing.<\/p>\n So, what exactly are iterables?<\/p>\n In Python, it is a special type of object that may be iterated over. That is, you can traverse through all of the various elements or entities present within the object or entity. It is simple to accomplish using for loops.<\/p>\n All iterable items have two special methods called __iter__() or __getitem__() that implement Sequence Semantics.<\/p>\n Lists, tuples, strings etc are examples of iterables in python.<\/p>\n Example<\/strong><\/p>\n 1)For Lists<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n 2)For Strings<\/strong><\/p>\n Example<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Now that we know what Iterables are in Python. How does this relate to indexing?<\/p>\n Indexing in Python is a method of referring\u00a0the individual items within an iterable based on their position.<\/p>\n In other words, you can directly access the desired elements within an iterable and perform various operations based on your requirements.<\/p>\n Note:\u00a0<\/strong><\/p>\n Objects in Python are “zero-indexed,” which means the position count starts at zero. A similar pattern is followed by several other programming languages. For example C, C++, java, etc, Let\u00a0 array<\/strong> =\u00a0 \u00a0 \u00a010\u00a0 11\u00a0 12\u00a0 13\u00a0 14<\/p>\n indexes :\u00a0 \u00a0<\/strong>\u00a0 \u00a0 \u00a0 \u00a00\u00a0 \u00a0 1\u00a0 \u00a0 2\u00a0 \u00a03\u00a0 \u00a0 4<\/strong><\/p>\n Example<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n When the index() method on a list is invoked with the item name as an argument, the index of a specific item within the list is displayed.<\/p>\n Now, we will see how to use the index() method on iterable objects.<\/p>\n The Python Index Operator is represented by opening and closing square brackets [ ].<\/strong><\/p>\n Syntax:<\/strong><\/p>\n n<\/strong> – It is an Integer. It is representing the position of the element we want to access.<\/p>\n Example<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n We just learnt how to use indexing in Lists and Strings to find specific objects of interest. Although we’ve used a positive integer inside our index operator (the square brackets) in all of our prior examples, this isn’t always necessary.<\/p>\n Negative integers are useful when we want to find the last few items of a list or when we want to index the list from the opposite end. Negative Indexing refers to the process of indexing from the opposite end.<\/p>\n Note: It should be noted that with negative indexing, the last element is represented by -1<\/strong> rather than -0.<\/strong><\/p>\n Output:<\/strong><\/p>\n What exactly is indexing in Python? \u2013 As simple as it appears, explaining how indexing works in Python can be a little tricky. So take a seat and read this article\u00a0to gain a better understanding of indexing in Python. Iterables: Before we get started with indexing, let’s define iterables and their primary function. Iterable knowledge …<\/p>\n\n
# Give the list as static input and store it in a variable.\r\n# List is an iterable\r\ngvn_lst = [1, 2, 3, 4, 5]\r\n# Iterate in the above list using the for loop.\r\nfor elemnt in gvn_lst:\r\n # Inside the for loop, print the element of the given list by printing the\r\n # iterator value.\r\n print(elemnt)\r\n<\/pre>\n
1\r\n2\r\n3\r\n4\r\n5<\/pre>\n
\n
# Give the string as static input and store it in a variable.\r\n# Strings are also examples of an iterable.\r\ngvn_str = \"Python-programs\"\r\n# Iterate in the above string using the for loop.\r\nfor chrctr in gvn_str:\r\n # Inside the for loop, print the characters of the given string by printing the\r\n # iterator value.\r\n print(chrctr)\r\n<\/pre>\n
P\r\ny\r\nt\r\nh\r\no\r\nn\r\n-\r\np\r\nr\r\no\r\ng\r\nr\r\na\r\nm\r\ns<\/pre>\n
Indexing in Python<\/h2>\n
\nSo, let’s say there are 6 elements in a list. The first element (i.e. the leftmost element) then takes the “zeroth” position, followed by the elements in the first, second, third, fourth, and fifth positions.<\/p>\n\n
# Give the list as static input and store it in a variable.\r\ngvn_lst = [\"welcome\", \"to\", \"Python\", \"programs\"]\r\n# Apply index() function to the given list by passing \"Python\" as an argument\r\n# to it to get index(position) of \"Python\"\r\n# Store it in a variable.\r\nindx = gvn_lst.index(\"Python\")\r\n# Print the index of \"Python\".\r\nprint(\"The Index of Python = \", indx)\r\n# Similarly, print the index of \"welcome\".\r\nprint(\"The Index of welcome = \", gvn_lst.index(\"welcome\"))\r\n<\/pre>\n
The Index of Python = 2\r\nThe Index of welcome = 0<\/pre>\n
Index Operator in Python<\/h4>\n
objectname[n]<\/pre>\n
\n
# Give the string as static input and store it in a variable.\r\ngvn_str = \"Python-programs\"\r\n# Pass some random number to the given string to get the element present\r\n# at the given index number.\r\n# Store it in another variable.\r\nelemnt = gvn_str[4]\r\n# Print the element present at the 4th index.\r\nprint(elemnt)\r\n# Similarly, try for the other numbers.\r\nprint(gvn_str[6])\r\nprint(gvn_str[2])\r\nprint(gvn_str[8])\r\n<\/pre>\n
o\r\n-\r\nt\r\nr<\/pre>\n
Negative Indexing<\/h4>\n
# Give the list as static input and store it in a variable.\r\ngvn_lst = [\"welcome\", \"to\", \"Python\", \"programs\"]\r\n# Using negative indexing to print the last item in a given list\r\nprint(gvn_lst[-1])\r\n# Printing the first item in a given list\r\nprint(gvn_lst[-4])\r\n# Printing the last second item in a given list\r\nprint(gvn_lst[-3])\r\n<\/pre>\n
programs\r\nwelcome\r\nto<\/pre>\n","protected":false},"excerpt":{"rendered":"