{"id":6323,"date":"2021-05-24T08:51:15","date_gmt":"2021-05-24T03:21:15","guid":{"rendered":"https:\/\/python-programs.com\/?p=6323"},"modified":"2021-11-22T18:53:38","modified_gmt":"2021-11-22T13:23:38","slug":"python-select-an-element-or-sub-array-by-index-from-a-numpy-array","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-select-an-element-or-sub-array-by-index-from-a-numpy-array\/","title":{"rendered":"Python: Select an Element or Sub Array by Index From a Numpy Array"},"content":{"rendered":"

Select an element or subarray by index from a Numpy array<\/h2>\n

In this article, we will discuss how we can access elements of numpy array using indexes and how to access subarray of Numpy array using slicing or range of indexes.<\/p>\n

Access element of Numpy array using indexes<\/h3>\n

As we all know array is a data structure in which elements are stored in a contiguous memory location. Hence it is easy to access array elements using an index. The same is with the case of the Numpy array. We can access elements of the Numpy array using indexes. As we implement this in python so we can access array elements using both positive and negative indexes.<\/p>\n

Positive index starts from 0 and it used to access the first element and using index 1,2,3………. we can access further elements. Negative index start from -1 and it used to access the last element and using index -2,-3,-4……… we can access furthermost elements. Let see this with the help of an example.<\/p>\n

import numpy as np\r\n#creating Numpy array\r\nnpArray=np.array([1, 2, 3, 4, 5,6,7,8,9,10])\r\nprint(npArray)\r\n#np[0] acess first element\r\nprint(npArray[0])\r\n#np[-1] acess last element \r\nprint(npArray[-1])\r\n#np[3] access 4th element from start\r\nprint(npArray[3])\r\n#np[-3] access 3rd element from last\r\nprint(npArray[-3])<\/pre>\n

Output<\/p>\n

[ 1  2  3  4  5  6  7  8  9 10]\r\n1\r\n10\r\n4\r\n8<\/span><\/pre>\n

Access subarray of Numpy array using slicing or range of indexes<\/h3>\n

When we study the list in python we see that we can access the subarray of the list using slicing. Its syntax looks like this:<\/p>\n

Suppose L is a list we can access the subarray of the list using L[a:b] where a denote starting index while b-1 denotes the last index of the subarray. In a similar way, we can implement this concept in a Numpy array.<\/p>\n

Now we see different structures of slicing for positive index<\/p>\n

1) L[a:b]-> a denote starting index of numpy array and b-1 denotes last index of numpy array.<\/p>\n

import numpy as np\r\n#creating Numpy array\r\nnpArray=np.array([1, 2, 3, 4, 5,6,7,8,9,10])\r\nprint(npArray)\r\n#Here we start from 3rd index and stop at 5th index\r\nprint(npArray[3:6])<\/pre>\n

Output<\/p>\n

[ 1  2  3  4  5  6  7  8  9 10]\r\n[4 5 6]<\/span><\/pre>\n

2) L[:b]-> Here a becomes starting index of the whole array i.e a is equal to zero and b-1 denotes the last index of the numpy array.<\/p>\n

import numpy as np\r\n#creating Numpy array\r\nnpArray=np.array([1, 2, 3, 4, 5,6,7,8,9,10])\r\nprint(npArray)\r\n#Here we start from 0th index and stop at 5th index\r\nprint(npArray[:6])<\/pre>\n

Output<\/p>\n

[ 1  2  3  4  5  6  7  8  9 10]\r\n<\/span>[1 2 3 4 5 6]<\/span><\/pre>\n

3) L[a:]-> a denote starting index of the numpy array and b becomes the last index of the whole array.<\/p>\n

import numpy as np\r\n#creating Numpy array\r\nnpArray=np.array([1, 2, 3, 4, 5,6,7,8,9,10])\r\nprint(npArray)\r\n#Here we start from 2nd index and stop at last index\r\nprint(npArray[2:])<\/pre>\n

Output<\/p>\n

[ 1  2  3  4  5  6  7  8  9 10]\r\n<\/span>[ 3  4  5  6  7  8  9 10]<\/span><\/pre>\n

4) L[a:b:c] -> a denote starting index of numpy array and b-1 denotes the last index of numpy array and c-1 denote how many elements we have to skip in between. The default value of c is 1.<\/p>\n

import numpy as np\r\n#creating Numpy array\r\nnpArray=np.array([1, 2, 3, 4, 5,6,7,8,9,10])\r\nprint(npArray)\r\n#Here we start from 2nd index and stop at sixth index and leave 1 element in  between\r\nprint(npArray[2:7:2])<\/pre>\n

Output<\/p>\n

[ 1  2  3  4  5  6  7  8  9 10]\r\n<\/span>[3 5 7]<\/span><\/pre>\n

5) L[a::c] -> a denote starting index of the numpy array and b becomes the last index of the whole array and c-1 denotes how many elements we have to skip in between. The default value of c is 1.<\/p>\n

import numpy as np\r\n#creating Numpy array\r\nnpArray=np.array([1, 2, 3, 4, 5,6,7,8,9,10])\r\nprint(npArray)\r\n#Here we start from 2nd index and stop at last index and leave 1 element in  between\r\nprint(npArray[2::2])<\/pre>\n

Output<\/p>\n

[ 1  2  3  4  5  6  7  8  9 10]\r\n<\/span>[3 5 7 9]<\/span><\/pre>\n

Now we see different structures of slicing for the Negative index<\/p>\n

1) L[a:b:c]-> a denote starting index of numpy array and b denotes last index of numpy array.Here c=-1 means we have to skip 0 elements,c=-2 means we have to skip 1 element, and so on<\/p>\n

import numpy as np\r\n#creating Numpy array\r\nnpArray=np.array([1, 2, 3, 4, 5,6,7,8,9,10])\r\nprint(npArray)\r\n#Here we start from 1st index from last and stop at 5th index from last \r\nprint(npArray[-1:-5:-1])<\/pre>\n

Output<\/p>\n

[ 1  2  3  4  5  6  7  8  9 10]\r\n<\/span>[10  9  8  7]<\/span><\/pre>\n

2) L[a::c]-> a denote starting index of the numpy array and b becomes the last index of the whole array. Here c=-1 means we have to skip 0 elements,c=-2 means we have to skip 1 element, and so on.<\/p>\n

import numpy as np\r\n#creating Numpy array\r\nnpArray=np.array([1, 2, 3, 4, 5,6,7,8,9,10])\r\nprint(npArray)\r\n#Here we start from last index and stop at 5th index from last leaving 1 element\r\nprint(npArray[:-5:-2])<\/pre>\n

Output<\/p>\n

[ 1  2  3  4  5  6  7  8  9 10]\r\n[10  8]<\/span><\/pre>\n

3) 5) L[a::c] -> a denote starting index of the numpy array and b becomes the last index of the whole array. Here c=-1 means we have to skip 0 elements,c=-2 means we have to skip 1 element, and so on.<\/p>\n

import numpy as np\r\n#creating Numpy array\r\nnpArray=np.array([1, 2, 3, 4, 5,6,7,8,9,10])\r\nprint(npArray)\r\n#Here we start from second index from last and stop at last index from last leaving 1 element\r\nprint(npArray[-2::-2])<\/pre>\n

Output<\/p>\n

[ 1  2  3  4  5  6  7  8  9 10]\r\n<\/span>[9 7 5 3 1]<\/span><\/pre>\n

So these are the methods select an element or subarray by index from a Numpy array.<\/p>\n","protected":false},"excerpt":{"rendered":"

Select an element or subarray by index from a Numpy array In this article, we will discuss how we can access elements of numpy array using indexes and how to access subarray of Numpy array using slicing or range of indexes. Access element of Numpy array using indexes As we all know array is a …<\/p>\n

Python: Select an Element or Sub Array by Index From a Numpy Array<\/span> Read More »<\/a><\/p>\n","protected":false},"author":8,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true},"categories":[5],"tags":[],"yoast_head":"\nPython: Select an Element or Sub Array by Index From a Numpy Array - Python Programs<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/python-programs.com\/python-select-an-element-or-sub-array-by-index-from-a-numpy-array\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python: Select an Element or Sub Array by Index From a Numpy Array - Python Programs\" \/>\n<meta property=\"og:description\" content=\"Select an element or subarray by index from a Numpy array In this article, we will discuss how we can access elements of numpy array using indexes and how to access subarray of Numpy array using slicing or range of indexes. Access element of Numpy array using indexes As we all know array is a … Python: Select an Element or Sub Array by Index From a Numpy Array Read More »\" \/>\n<meta property=\"og:url\" content=\"https:\/\/python-programs.com\/python-select-an-element-or-sub-array-by-index-from-a-numpy-array\/\" \/>\n<meta property=\"og:site_name\" content=\"Python Programs\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/btechgeeks\" \/>\n<meta property=\"article:published_time\" content=\"2021-05-24T03:21:15+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-11-22T13:23:38+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@btech_geeks\" \/>\n<meta name=\"twitter:site\" content=\"@btech_geeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Mayank Gupta\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Organization\",\"@id\":\"https:\/\/python-programs.com\/#organization\",\"name\":\"BTech Geeks\",\"url\":\"https:\/\/python-programs.com\/\",\"sameAs\":[\"https:\/\/www.instagram.com\/btechgeeks\/\",\"https:\/\/www.linkedin.com\/in\/btechgeeks\",\"https:\/\/in.pinterest.com\/btechgeek\/\",\"https:\/\/www.youtube.com\/channel\/UC9MlCqdJ3lKqz2p5114SDIg\",\"https:\/\/www.facebook.com\/btechgeeks\",\"https:\/\/twitter.com\/btech_geeks\"],\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/python-programs.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png\",\"contentUrl\":\"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png\",\"width\":350,\"height\":70,\"caption\":\"BTech Geeks\"},\"image\":{\"@id\":\"https:\/\/python-programs.com\/#\/schema\/logo\/image\/\"}},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/python-programs.com\/#website\",\"url\":\"https:\/\/python-programs.com\/\",\"name\":\"Python Programs\",\"description\":\"Python Programs with Examples, How To Guides on Python\",\"publisher\":{\"@id\":\"https:\/\/python-programs.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/python-programs.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/python-programs.com\/python-select-an-element-or-sub-array-by-index-from-a-numpy-array\/#webpage\",\"url\":\"https:\/\/python-programs.com\/python-select-an-element-or-sub-array-by-index-from-a-numpy-array\/\",\"name\":\"Python: Select an Element or Sub Array by Index From a Numpy Array - Python Programs\",\"isPartOf\":{\"@id\":\"https:\/\/python-programs.com\/#website\"},\"datePublished\":\"2021-05-24T03:21:15+00:00\",\"dateModified\":\"2021-11-22T13:23:38+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/python-programs.com\/python-select-an-element-or-sub-array-by-index-from-a-numpy-array\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/python-programs.com\/python-select-an-element-or-sub-array-by-index-from-a-numpy-array\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/python-programs.com\/python-select-an-element-or-sub-array-by-index-from-a-numpy-array\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/python-programs.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python: Select an Element or Sub Array by Index From a Numpy Array\"}]},{\"@type\":\"Article\",\"@id\":\"https:\/\/python-programs.com\/python-select-an-element-or-sub-array-by-index-from-a-numpy-array\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/python-programs.com\/python-select-an-element-or-sub-array-by-index-from-a-numpy-array\/#webpage\"},\"author\":{\"@id\":\"https:\/\/python-programs.com\/#\/schema\/person\/2deaa2ec7f26a4b5e527737709032a41\"},\"headline\":\"Python: Select an Element or Sub Array by Index From a Numpy Array\",\"datePublished\":\"2021-05-24T03:21:15+00:00\",\"dateModified\":\"2021-11-22T13:23:38+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/python-programs.com\/python-select-an-element-or-sub-array-by-index-from-a-numpy-array\/#webpage\"},\"wordCount\":588,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/python-programs.com\/#organization\"},\"articleSection\":[\"Python\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/python-programs.com\/python-select-an-element-or-sub-array-by-index-from-a-numpy-array\/#respond\"]}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/python-programs.com\/#\/schema\/person\/2deaa2ec7f26a4b5e527737709032a41\",\"name\":\"Mayank Gupta\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/python-programs.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/d90409993b088dd1f731cb0e2d8b231c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/d90409993b088dd1f731cb0e2d8b231c?s=96&d=mm&r=g\",\"caption\":\"Mayank Gupta\"},\"url\":\"https:\/\/python-programs.com\/author\/mayank\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python: Select an Element or Sub Array by Index From a Numpy Array - Python Programs","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/python-programs.com\/python-select-an-element-or-sub-array-by-index-from-a-numpy-array\/","og_locale":"en_US","og_type":"article","og_title":"Python: Select an Element or Sub Array by Index From a Numpy Array - Python Programs","og_description":"Select an element or subarray by index from a Numpy array In this article, we will discuss how we can access elements of numpy array using indexes and how to access subarray of Numpy array using slicing or range of indexes. Access element of Numpy array using indexes As we all know array is a … Python: Select an Element or Sub Array by Index From a Numpy Array Read More »","og_url":"https:\/\/python-programs.com\/python-select-an-element-or-sub-array-by-index-from-a-numpy-array\/","og_site_name":"Python Programs","article_publisher":"https:\/\/www.facebook.com\/btechgeeks","article_published_time":"2021-05-24T03:21:15+00:00","article_modified_time":"2021-11-22T13:23:38+00:00","twitter_card":"summary_large_image","twitter_creator":"@btech_geeks","twitter_site":"@btech_geeks","twitter_misc":{"Written by":"Mayank Gupta","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Organization","@id":"https:\/\/python-programs.com\/#organization","name":"BTech Geeks","url":"https:\/\/python-programs.com\/","sameAs":["https:\/\/www.instagram.com\/btechgeeks\/","https:\/\/www.linkedin.com\/in\/btechgeeks","https:\/\/in.pinterest.com\/btechgeek\/","https:\/\/www.youtube.com\/channel\/UC9MlCqdJ3lKqz2p5114SDIg","https:\/\/www.facebook.com\/btechgeeks","https:\/\/twitter.com\/btech_geeks"],"logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/python-programs.com\/#\/schema\/logo\/image\/","url":"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png","contentUrl":"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png","width":350,"height":70,"caption":"BTech Geeks"},"image":{"@id":"https:\/\/python-programs.com\/#\/schema\/logo\/image\/"}},{"@type":"WebSite","@id":"https:\/\/python-programs.com\/#website","url":"https:\/\/python-programs.com\/","name":"Python Programs","description":"Python Programs with Examples, How To Guides on Python","publisher":{"@id":"https:\/\/python-programs.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/python-programs.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/python-programs.com\/python-select-an-element-or-sub-array-by-index-from-a-numpy-array\/#webpage","url":"https:\/\/python-programs.com\/python-select-an-element-or-sub-array-by-index-from-a-numpy-array\/","name":"Python: Select an Element or Sub Array by Index From a Numpy Array - Python Programs","isPartOf":{"@id":"https:\/\/python-programs.com\/#website"},"datePublished":"2021-05-24T03:21:15+00:00","dateModified":"2021-11-22T13:23:38+00:00","breadcrumb":{"@id":"https:\/\/python-programs.com\/python-select-an-element-or-sub-array-by-index-from-a-numpy-array\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/python-programs.com\/python-select-an-element-or-sub-array-by-index-from-a-numpy-array\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/python-programs.com\/python-select-an-element-or-sub-array-by-index-from-a-numpy-array\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/python-programs.com\/"},{"@type":"ListItem","position":2,"name":"Python: Select an Element or Sub Array by Index From a Numpy Array"}]},{"@type":"Article","@id":"https:\/\/python-programs.com\/python-select-an-element-or-sub-array-by-index-from-a-numpy-array\/#article","isPartOf":{"@id":"https:\/\/python-programs.com\/python-select-an-element-or-sub-array-by-index-from-a-numpy-array\/#webpage"},"author":{"@id":"https:\/\/python-programs.com\/#\/schema\/person\/2deaa2ec7f26a4b5e527737709032a41"},"headline":"Python: Select an Element or Sub Array by Index From a Numpy Array","datePublished":"2021-05-24T03:21:15+00:00","dateModified":"2021-11-22T13:23:38+00:00","mainEntityOfPage":{"@id":"https:\/\/python-programs.com\/python-select-an-element-or-sub-array-by-index-from-a-numpy-array\/#webpage"},"wordCount":588,"commentCount":0,"publisher":{"@id":"https:\/\/python-programs.com\/#organization"},"articleSection":["Python"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/python-programs.com\/python-select-an-element-or-sub-array-by-index-from-a-numpy-array\/#respond"]}]},{"@type":"Person","@id":"https:\/\/python-programs.com\/#\/schema\/person\/2deaa2ec7f26a4b5e527737709032a41","name":"Mayank Gupta","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/python-programs.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/d90409993b088dd1f731cb0e2d8b231c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/d90409993b088dd1f731cb0e2d8b231c?s=96&d=mm&r=g","caption":"Mayank Gupta"},"url":"https:\/\/python-programs.com\/author\/mayank\/"}]}},"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts\/6323"}],"collection":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/users\/8"}],"replies":[{"embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/comments?post=6323"}],"version-history":[{"count":3,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts\/6323\/revisions"}],"predecessor-version":[{"id":6326,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts\/6323\/revisions\/6326"}],"wp:attachment":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/media?parent=6323"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/categories?post=6323"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/tags?post=6323"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}