{"id":3381,"date":"2020-05-28T14:48:19","date_gmt":"2020-05-28T13:48:19","guid":{"rendered":"https:\/\/www.itersdesktop.com\/?p=3381"},"modified":"2020-05-28T15:35:12","modified_gmt":"2020-05-28T14:35:12","slug":"fastest-way-to-get-the-first-n-elements-of-a-list-or-an-array","status":"publish","type":"post","link":"https:\/\/www.itersdesktop.com\/vi\/2020\/05\/28\/fastest-way-to-get-the-first-n-elements-of-a-list-or-an-array\/","title":{"rendered":"Fastest way to get the first n elements of a List into an Array"},"content":{"rendered":"<p><\/p>\n<p>def s = &#8216;Hello Groovy world!&#8217;<br \/>\nassert s.take(5) == &#8216;Hello&#8217;<br \/>\nassert s.drop(6) == &#8216;Groovy world!&#8217;<\/p>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_83 counter-hierarchy ez-toc-counter ez-toc-light-blue ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a14211c297c8\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a14211c297c8\"  aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><ul class='ez-toc-list-level-2' ><li class='ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.itersdesktop.com\/vi\/2020\/05\/28\/fastest-way-to-get-the-first-n-elements-of-a-list-or-an-array\/#python\" >Python<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.itersdesktop.com\/vi\/2020\/05\/28\/fastest-way-to-get-the-first-n-elements-of-a-list-or-an-array\/#slicing-the-list\" >Slicing the list<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.itersdesktop.com\/vi\/2020\/05\/28\/fastest-way-to-get-the-first-n-elements-of-a-list-or-an-array\/#slicing-a-generator\" >Slicing a generator<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/www.itersdesktop.com\/vi\/2020\/05\/28\/fastest-way-to-get-the-first-n-elements-of-a-list-or-an-array\/#javascript\" >JavaScript<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/www.itersdesktop.com\/vi\/2020\/05\/28\/fastest-way-to-get-the-first-n-elements-of-a-list-or-an-array\/#summary\" >Summary<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/www.itersdesktop.com\/vi\/2020\/05\/28\/fastest-way-to-get-the-first-n-elements-of-a-list-or-an-array\/#references\" >References<\/a><ul class='ez-toc-list-level-2' ><li class='ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/www.itersdesktop.com\/vi\/2020\/05\/28\/fastest-way-to-get-the-first-n-elements-of-a-list-or-an-array\/#python-2\" >Python<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/www.itersdesktop.com\/vi\/2020\/05\/28\/fastest-way-to-get-the-first-n-elements-of-a-list-or-an-array\/#slicing-the-list-2\" >Slicing the list<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/www.itersdesktop.com\/vi\/2020\/05\/28\/fastest-way-to-get-the-first-n-elements-of-a-list-or-an-array\/#slicing-a-generator-2\" >Slicing a generator<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/www.itersdesktop.com\/vi\/2020\/05\/28\/fastest-way-to-get-the-first-n-elements-of-a-list-or-an-array\/#javascript-2\" >JavaScript<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/www.itersdesktop.com\/vi\/2020\/05\/28\/fastest-way-to-get-the-first-n-elements-of-a-list-or-an-array\/#summary-2\" >Summary<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/www.itersdesktop.com\/vi\/2020\/05\/28\/fastest-way-to-get-the-first-n-elements-of-a-list-or-an-array\/#references-2\" >References<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/www.itersdesktop.com\/vi\/2020\/05\/28\/fastest-way-to-get-the-first-n-elements-of-a-list-or-an-array\/#introduction-to-a-feasible-approach\" >Introduction to a feasible approach<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-14\" href=\"https:\/\/www.itersdesktop.com\/vi\/2020\/05\/28\/fastest-way-to-get-the-first-n-elements-of-a-list-or-an-array\/#how-to-implement-the-solution\" >How to implement the solution<\/a><ul class='ez-toc-list-level-2' ><li class='ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-15\" href=\"https:\/\/www.itersdesktop.com\/vi\/2020\/05\/28\/fastest-way-to-get-the-first-n-elements-of-a-list-or-an-array\/#java\" >Java<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-16\" href=\"https:\/\/www.itersdesktop.com\/vi\/2020\/05\/28\/fastest-way-to-get-the-first-n-elements-of-a-list-or-an-array\/#using-a-traditional-for-loop\" >Using a traditional for loop<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-17\" href=\"https:\/\/www.itersdesktop.com\/vi\/2020\/05\/28\/fastest-way-to-get-the-first-n-elements-of-a-list-or-an-array\/#using-the-built-in-method-sublist\" >Using the built-in method subList<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-18\" href=\"https:\/\/www.itersdesktop.com\/vi\/2020\/05\/28\/fastest-way-to-get-the-first-n-elements-of-a-list-or-an-array\/#using-java-8-streams\" >Using Java 8 Streams<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-19\" href=\"https:\/\/www.itersdesktop.com\/vi\/2020\/05\/28\/fastest-way-to-get-the-first-n-elements-of-a-list-or-an-array\/#groovy\" >Groovy<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-20\" href=\"https:\/\/www.itersdesktop.com\/vi\/2020\/05\/28\/fastest-way-to-get-the-first-n-elements-of-a-list-or-an-array\/#python-3\" >Python<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-21\" href=\"https:\/\/www.itersdesktop.com\/vi\/2020\/05\/28\/fastest-way-to-get-the-first-n-elements-of-a-list-or-an-array\/#slicing-the-list-3\" >Slicing the list<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-22\" href=\"https:\/\/www.itersdesktop.com\/vi\/2020\/05\/28\/fastest-way-to-get-the-first-n-elements-of-a-list-or-an-array\/#slicing-a-generator-3\" >Slicing a generator<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-23\" href=\"https:\/\/www.itersdesktop.com\/vi\/2020\/05\/28\/fastest-way-to-get-the-first-n-elements-of-a-list-or-an-array\/#javascript-3\" >JavaScript<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-24\" href=\"https:\/\/www.itersdesktop.com\/vi\/2020\/05\/28\/fastest-way-to-get-the-first-n-elements-of-a-list-or-an-array\/#summary-3\" >Summary<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-25\" href=\"https:\/\/www.itersdesktop.com\/vi\/2020\/05\/28\/fastest-way-to-get-the-first-n-elements-of-a-list-or-an-array\/#references-3\" >References<\/a><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"python\"><\/span>Python<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Let&#8217;s assume an arbitrary list\/array:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">in_list = list(range(10))\r\n\/\/ Python2: in_list = range(10)\r\n\/\/ in_array = [i for i in range(10)]<\/pre>\n<h3><span class=\"ez-toc-section\" id=\"slicing-the-list\"><\/span>Slicing the list<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">n = 5\r\ntop5 = in_list[:5]<\/pre>\n<ul>\n<li>To slice a list, there&#8217;s a simple syntax:\u00a0<code class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">array[start:stop:step]<\/code><\/li>\n<li>You can omit any parameter. These are all valid:\u00a0<code class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">array[start:]<\/code>,\u00a0<code>array[:stop]<\/code>,\u00a0<code>array[::step]<\/code><\/li>\n<\/ul>\n<h3><span class=\"ez-toc-section\" id=\"slicing-a-generator\"><\/span>Slicing a generator<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>&nbsp;<\/p>\n<h2><span class=\"ez-toc-section\" id=\"javascript\"><\/span>JavaScript<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h1><span class=\"ez-toc-section\" id=\"summary\"><\/span>Summary<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<p>From my perspective, this operation is often requested in real applications. Beware of existing solutions is probably useful and necessary because we could need either of them in your coming projects.<\/p>\n<h1><span class=\"ez-toc-section\" id=\"references\"><\/span>References<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<ol>\n<li style=\"text-align: left;\"><a href=\"https:\/\/stackoverflow.com\/questions\/31330330\/fastest-way-to-get-the-first-n-elements-of-a-list-into-an-array\" target=\"_blank\" rel=\"noopener noreferrer\">Fastest way to get the first n elements of a List into an Array<\/a>, accessed on 20\/05\/2020<\/li>\n<li><a href=\"https:\/\/mrhaki.blogspot.com\/2011\/09\/groovy-goodness-take-and-drop-items.html\" target=\"_blank\" rel=\"noopener noreferrer\">Groovy Goodness: Take and Drop Items from a List<\/a>, accessed on 20\/05\/2020<\/li>\n<li><a href=\"https:\/\/stackoverflow.com\/questions\/5234090\/how-to-take-the-first-n-items-from-a-generator-or-list-in-python\" target=\"_blank\" rel=\"noopener noreferrer\">How to take the first N items from a generator or list in Python?<\/a>, accessed on 28\/05\/2020<\/li>\n<\/ol>\n<p><\/p>","protected":false},"excerpt":{"rendered":"<p>def s = &#8216;Hello Groovy world!&#8217; assert s.take(5) == &#8216;Hello&#8217; assert s.drop(6) == &#8216;Groovy world!&#8217; Python Let&#8217;s assume an arbitrary list\/array: in_list = list(range(10)) \/\/ Python2: in_list = range(10) \/\/&hellip; <\/p>\n","protected":false},"author":2,"featured_media":3541,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[348,595,28,641,230,122],"tags":[349,648,347],"class_list":["post-3381","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-data-structures","category-groovy","category-java","category-javascript","category-programming","category-python","tag-array","tag-arraylist","tag-list"],"_links":{"self":[{"href":"https:\/\/www.itersdesktop.com\/vi\/wp-json\/wp\/v2\/posts\/3381","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.itersdesktop.com\/vi\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.itersdesktop.com\/vi\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.itersdesktop.com\/vi\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.itersdesktop.com\/vi\/wp-json\/wp\/v2\/comments?post=3381"}],"version-history":[{"count":7,"href":"https:\/\/www.itersdesktop.com\/vi\/wp-json\/wp\/v2\/posts\/3381\/revisions"}],"predecessor-version":[{"id":3543,"href":"https:\/\/www.itersdesktop.com\/vi\/wp-json\/wp\/v2\/posts\/3381\/revisions\/3543"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.itersdesktop.com\/vi\/wp-json\/wp\/v2\/media\/3541"}],"wp:attachment":[{"href":"https:\/\/www.itersdesktop.com\/vi\/wp-json\/wp\/v2\/media?parent=3381"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.itersdesktop.com\/vi\/wp-json\/wp\/v2\/categories?post=3381"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.itersdesktop.com\/vi\/wp-json\/wp\/v2\/tags?post=3381"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}