Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9210797
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T01:09:32+00:00 2026-06-18T01:09:32+00:00

I am trying to dynamically get the first and last element from an array.

  • 0

I am trying to dynamically get the first and last element from an array.

So, let us suppose the array has 6 elements.

test = [1,23,4,6,7,8]

If I am trying to get the first and last = 1,8, 23,7 and 4,6. Is there a way to get elements in this order?
I looked at a couple of questions Link Link2. I took help of these links and I came up with this prototype..

#!/usr/bin/env python

import numpy

test = [1,23,4,6,7,8]
test1 = numpy.array([1,23,4,6,7,8])
len_test = len(test)
first_list = [0,1,2]
len_first = len(first_list)
second_list = [-1,-2,-3]
len_second = len(second_list)

for a in range(len_first):
        print numpy.array(test)[[first_list[a] , second_list[a]]]
        print test1[[first_list[a], second_list[a]]]

But this prototype won’t scale for if you have more than 6 elements. So, I was wondering if there is way to dynamically get the pair of elements.

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T01:09:33+00:00Added an answer on June 18, 2026 at 1:09 am

    How about:

    In [10]: arr = numpy.array([1,23,4,6,7,8])
    
    In [11]: [(arr[i], arr[-i-1]) for i in range(len(arr) // 2)]
    Out[11]: [(1, 8), (23, 7), (4, 6)]
    

    Depending on the size of arr, writing the entire thing in NumPy may be more performant:

    In [41]: arr = numpy.array([1,23,4,6,7,8]*100)
    
    In [42]: %timeit [(arr[i], arr[-i-1]) for i in range(len(arr) // 2)]
    10000 loops, best of 3: 167 us per loop
    
    In [43]: %timeit numpy.vstack((arr, arr[::-1]))[:,:len(arr)//2]
    100000 loops, best of 3: 16.4 us per loop
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to get an dynamically created element using a jQuery selector but
I am trying to dynamically request a file for download via a GET ajax
I am trying to get a new dynamically created row to add to a
I am trying to get post a form to a hidden, dynamically inserted iframe,
I'm trying to dynamically create a navbar from data i fetch from a web
I'm trying to dynamically bind mysql_stmt parameters and get the result in an associative
I'm trying to dynamically style some elements on my pages. So, if I had
I'm trying to get an understanding of how to dynamically add columns and rows
trying to get some thing like that dynamically <ul> <li><a href=index.php>Home</a></li> <li><a href=about.php>About Us</a></li>
REVISED...ADDITIONAL INFO ADDED AT BOTTOM... I am trying to get a dynamically created, nested

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.