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 690869
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:28:51+00:00 2026-05-14T02:28:51+00:00

I have a NumPy array [1,2,3,4,5,6,7,8,9,10,11,12,13,14] and want to have an array structured like

  • 0

I have a NumPy array [1,2,3,4,5,6,7,8,9,10,11,12,13,14] and want to have an array structured like [[1,2,3,4], [2,3,4,5], [3,4,5,6], ..., [11,12,13,14]].

Sure this is possible by looping over the large array and adding arrays of length four to the new array, but I’m curious if there is some secret ‘magic’ Python method doing just this 🙂

  • 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-05-14T02:28:51+00:00Added an answer on May 14, 2026 at 2:28 am

    The fastest way seems to be to preallocate the array, given as option 7 right at the bottom of this answer.

    >>> import numpy as np
    >>> A=np.array([1,2,3,4,5,6,7,8,9,10,11,12,13,14])
    >>> A
    array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14])
    >>> np.array(zip(A,A[1:],A[2:],A[3:]))
    array([[ 1,  2,  3,  4],
           [ 2,  3,  4,  5],
           [ 3,  4,  5,  6],
           [ 4,  5,  6,  7],
           [ 5,  6,  7,  8],
           [ 6,  7,  8,  9],
           [ 7,  8,  9, 10],
           [ 8,  9, 10, 11],
           [ 9, 10, 11, 12],
           [10, 11, 12, 13],
           [11, 12, 13, 14]])
    >>> 
    

    You can easily adapt this to do it for variable chunk size.

    >>> n=5
    >>> np.array(zip(*(A[i:] for i in range(n))))
    array([[ 1,  2,  3,  4,  5],
           [ 2,  3,  4,  5,  6],
           [ 3,  4,  5,  6,  7],
           [ 4,  5,  6,  7,  8],
           [ 5,  6,  7,  8,  9],
           [ 6,  7,  8,  9, 10],
           [ 7,  8,  9, 10, 11],
           [ 8,  9, 10, 11, 12],
           [ 9, 10, 11, 12, 13],
           [10, 11, 12, 13, 14]])
    

    You may wish to compare performance between this and using itertools.islice.

    >>> from itertools import islice
    >>> n=4
    >>> np.array(zip(*[islice(A,i,None) for i in range(n)]))
    array([[ 1,  2,  3,  4],
           [ 2,  3,  4,  5],
           [ 3,  4,  5,  6],
           [ 4,  5,  6,  7],
           [ 5,  6,  7,  8],
           [ 6,  7,  8,  9],
           [ 7,  8,  9, 10],
           [ 8,  9, 10, 11],
           [ 9, 10, 11, 12],
           [10, 11, 12, 13],
           [11, 12, 13, 14]])
    

    My timing results:

    1. timeit np.array(zip(A,A[1:],A[2:],A[3:]))
    10000 loops, best of 3: 92.9 us per loop
    
    2. timeit np.array(zip(*(A[i:] for i in range(4))))
    10000 loops, best of 3: 101 us per loop
    
    3. timeit np.array(zip(*[islice(A,i,None) for i in range(4)]))
    10000 loops, best of 3: 101 us per loop
    
    4. timeit numpy.array([ A[i:i+4] for i in range(len(A)-3) ])
    10000 loops, best of 3: 37.8 us per loop
    
    5. timeit numpy.array(list(chunks(A, 4)))
    10000 loops, best of 3: 43.2 us per loop
    
    6. timeit numpy.array(byN(A, 4))
    10000 loops, best of 3: 100 us per loop
    
    # Does preallocation of the array help? (11 is from len(A)+1-4)
    7. timeit B=np.zeros(shape=(11, 4),dtype=np.int32)
    100000 loops, best of 3: 2.19 us per loop
       timeit for i in range(4):B[:,i]=A[i:11+i]
    10000 loops, best of 3: 20.9 us per loop
    total 23.1us per loop
    

    As len(A) increases (20000) 4 and 5 converge to be equivalent speed (44 ms). 1,2,3 and 6 all remain about 3 times slower (135 ms). 7 is much faster (1.36 ms).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a numpy array like this a = np.array(1) Now if I want
I have a multidimensional numpy array I'd like to iterate over. I want to
I have a numpy array like this: x = np.array([[1,2,3],[4,5,6],[7,8,9]]) I need to create
I have a 2d numpy array Something like this: [[ 1 2 3 4],
Assuming I have a numpy array like: [1,2,3,4,5,6] and another array: [0,0,1,2,2,1] I want
I have a large numpy array (8 by 30000) and I want to delete
I have a numpy.ndarray like this: array([[ 11.18033989, 0. ], [ 8.24621125, 3. ],
I have a numpy 2D array self.sub and i want to use it in
i have a numpy array like the following [[ 1 2 3 4 ]
This is a simplification of my question. I have a numpy array: x =

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.