{"id":8035,"date":"2024-03-19T06:52:17","date_gmt":"2024-03-19T06:52:17","guid":{"rendered":"https:\/\/www.skillvertex.com\/blog\/?p=8035"},"modified":"2024-03-19T06:52:17","modified_gmt":"2024-03-19T06:52:17","slug":"python-access-list-items","status":"publish","type":"post","link":"https:\/\/www.skillvertex.com\/blog\/python-access-list-items\/","title":{"rendered":"Python Access List Items"},"content":{"rendered":"\n<div class=\"wp-block-rank-math-toc-block\" id=\"rank-math-toc\" id=\"rank-math-toc\"><p>Table of Contents<\/p><nav><ul><li ><a href=\"#what-are-access-items\">What are Access Items?<\/a><\/li><li ><a href=\"#what-is-negative-indexing\">What is Negative Indexing?<\/a><\/li><li ><a href=\"#what-is-the-range-of-indexes\">What is the Range of Indexes?<\/a><\/li><li ><a href=\"#what-is-the-range-of-negative-indexes\">What is the Range of Negative Indexes?<\/a><\/li><li ><a href=\"#conclusion\">Conclusion<\/a><\/li><li ><a href=\"#python-access-list-items-fa-qs\">Python\u00a0Access List Items- FAQs<\/a><\/li><\/ul><\/nav><\/div>\n\n\n\n<p>In Python, the list is known as a sequence. Each object in the list will be accessible with its index. The index will begin from 0 and the last item in the list is the length-1. This article has listed the Python Access List Items.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-are-access-items\">What are Access Items?<\/h2>\n\n\n\n<p>In Python, we can access the list items by referring to the index number. It is required to use the square brackets for slicing along with the index or indices to retrieve the value that is available at the <a href=\"https:\/\/www.skillvertex.com\/blog\/which-of-the-following-is-valid-sql-for-an-index\/\" data-type=\"post\" data-id=\"1479\">index<\/a>.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n\n<p>The slice <a href=\"https:\/\/www.skillvertex.com\/blog\/sizeof-operator-in-c\/\" data-type=\"post\" data-id=\"2234\">operator <\/a>will get one or more items from the <a href=\"https:\/\/www.skillvertex.com\/blog\/python-lists\/\" data-type=\"post\" data-id=\"7938\">list<\/a>. So, you have to put the index on square brackets to get them at its position.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>obj = list1&#91;i]\n<\/code><\/pre>\n\n\n\n<p>Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Creating a sample list\nmy_list = &#91;1, 2, 3, 4, 5]\n\n# Accessing individual elements by index\nfirst_element = my_list\n<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\nFirst element: 1\nSecond element: 2\nThird element: 3\nLast element: 5\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-negative-indexing\">What is Negative Indexing?<\/h2>\n\n\n\n<p>The Negative Indexing will refer to the beginning from the end, that is  -1  will mean the last item and -2 will have the second last item.<\/p>\n\n\n\n<p>Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Creating a sample list\nmy_list = &#91;10, 20, 30, 40, 50]\n\n# Accessing elements using negative indexing\nlast_element = my_list&#91;-1]\nsecond_last_element = my_list&#91;-2]\nthird_last_element = my_list&#91;-3]\n\n# Printing the results\nprint(\"Last element:\", last_element)\nprint(\"Second last element:\", second_last_element)\nprint(\"Third last element:\", third_last_element)\n<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Last element: 50\nSecond last element: 40\nThird last element: 30\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-the-range-of-indexes\">What is the Range of Indexes?<\/h2>\n\n\n\n<p>In <a href=\"https:\/\/www.skillvertex.com\/blog\/mern-stack-vs-python\/\" data-type=\"post\" data-id=\"4258\">Python<\/a>, the range of the index will indicate where to begin and where to end the range. However, while providing the range, the return <a href=\"https:\/\/www.skillvertex.com\/blog\/how-to-pass-an-array-by-value-in-c\/\" data-type=\"post\" data-id=\"2638\">value<\/a> will be given a new list along with the specified items.<\/p>\n\n\n\n<p>Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Creating a sample list\nmy_list = &#91;10, 20, 30, 40, 50]\n\n# Accessing a range of elements using slicing\nstart_index = 1\nend_index = 4\nsubset_of_list = my_list&#91;start_index:end_index]\n\n# Printing the result\nprint(\"Subset of the list:\", subset_of_list)\n<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Subset of the list: &#91;20, 30, 40]\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-the-range-of-negative-indexes\">What is the Range of Negative Indexes?<\/h2>\n\n\n\n<p>In Python, it is important to specify indexes if you need to begin from the end of the list.<\/p>\n\n\n\n<p>Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Creating a sample list\nmy_list = &#91;10, 20, 30, 40, 50]\n\n# Accessing a range of elements using negative indexing\nstart_index = -3\nend_index = -1\nsubset_of_list = my_list&#91;start_index:end_index]\n\n# Printing the result\nprint(\"Subset of the list using negative indexing:\", subset_of_list)\n<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Subset of the list using negative indexing: &#91;30, 40]\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"conclusion\">Conclusion<\/h2>\n\n\n\n<p>To conclude, this article has illustrated about the Python Access List Items. It has also included several examples of negative indexing and a range of items.  Students can improve their knowledge and coding skills after learning the Python Access list items.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"python-access-list-items-fa-qs\">Python&nbsp;Access List Items- FAQs<\/h2>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1709806512136\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q1. How do you access a list of objects in Python?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. In  Python, you can access the list items by referring to the index number.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1709806519715\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q2.How do you select an item from a list in Python?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. The list of Python list elements will be selected the separated by the starting and ending points.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1709806526747\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q3. How do you access elements in a list?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans.Elements can be accessed using the index operator([]).<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Python, the list is known as a sequence. Each object in the list will be accessible with its index. The index will begin from 0 and the last item in the list is the length-1. This article has listed the Python Access List Items. What are Access Items? In Python, we can access the &#8230; <a title=\"Python Access List Items\" class=\"read-more\" href=\"https:\/\/www.skillvertex.com\/blog\/python-access-list-items\/\" aria-label=\"More on Python Access List Items\">Read more<\/a><\/p>\n","protected":false},"author":4,"featured_media":8041,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[864],"tags":[57,966,866],"class_list":["post-8035","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-tutorial","tag-python","tag-python-access-list","tag-python-tutorial","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-33"],"_links":{"self":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/8035","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/comments?post=8035"}],"version-history":[{"count":10,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/8035\/revisions"}],"predecessor-version":[{"id":8291,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/8035\/revisions\/8291"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media\/8041"}],"wp:attachment":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media?parent=8035"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/categories?post=8035"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/tags?post=8035"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}