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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T01:29:38+00:00 2026-06-12T01:29:38+00:00

I have array [1,2,1,2,3,4,3,4,1,2] I want to loop it x times, each times moving

  • 0

I have array [1,2,1,2,3,4,3,4,1,2]

I want to loop it x times, each times moving array every element 1 position forward:

So next loop will be:

2.[2,1,2,3,4,3,4,1,2,1]

3.
[1,2,3,4,3,4,1,2,1,2]

etc….

How can I manipulate array like this way?

EDIT:

What I thought, but maybe some better tricks:

Just go over the array with while loop and create new array with for cycle.

for i in range(11)
    array[i] = array[i-1]

etc etc..its pseudo code

  • 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-12T01:29:39+00:00Added an answer on June 12, 2026 at 1:29 am

    Using a List data structure isn’t an efficient way to do this. A Queue would be more appropriate. In any case:

    Using a Queue

    As I suggested, using a Queue (collections.deque):

    >>> q = collections.deque([1,2,3,4,5,6,7,8])
    >>> for _ in xrange(5):
    ...     q.rotate(-1)
    ... 
    >>> q
    deque([6, 7, 8, 1, 2, 3, 4, 5])
    

    Keeping the List

    >>> a = [1,2,3,4,5,6,7,8]
    >>> for _ in xrange(5):
    ...     a = a[1:] + a[:1]
    ... 
    >>> a
    [6, 7, 8, 1, 2, 3, 4, 5]
    

    Alternatively (faster than the previous one):

    >>> a = [1,2,3,4,5,6,7,8]
    >>> for _ in xrange(5):
    ...     a.append(a.pop(0))
    ... 
    >>> a
    [6, 7, 8, 1, 2, 3, 4, 5]
    

    Here you can change xrange for whatever you want to iterate over.

    Timeit analysis:

    Pop-Append

    >>> timeit.timeit('a.append(a.pop(0))', setup='a = [0,1,2,3,4,5,6,7,8,9]', number=1000000)
    0.24548697471618652
    >>> timeit.timeit('a.append(a.pop(0))', setup='a = [0,1,2,3,4,5,6,7,8,9]', number=100000000)
    23.65538215637207
    

    Slicing

    >>> timeit.timeit('a=a[1:] + a[:1]', setup='a = [0,1,2,3,4,5,6,7,8,9]', number=1000000)
    0.36037278175354004
    >>> timeit.timeit('a=a[1:] + a[:1]', setup='a = [0,1,2,3,4,5,6,7,8,9]', number=100000000)
    35.06173801422119
    

    Queue

    >>> timeit.timeit('q.rotate(-1)', setup='import collections; q = collections.deque([0,1,2,3,4,5,6,7,8])', number=1000000)
    0.16829514503479004
    >>> timeit.timeit('q.rotate(-1)', setup='import collections; q = collections.deque([0,1,2,3,4,5,6,7,8])', number=100000000)
    16.012277841567993
    

    With a little optimization, basically removing the __getattr__ call for append, pop and rotate:

    Pop-Append

    >>> timeit.timeit('aa(ap(0))', setup='a = [0,1,2,3,4,5,6,7,8,9]; aa=a.append; ap=a.pop', number=1000000)
    0.15255093574523926
    >>> timeit.timeit('aa(ap(0))', setup='a = [0,1,2,3,4,5,6,7,8,9]; aa=a.append; ap=a.pop', number=100000000)
    14.50795292854309
    

    Queue

    >>> timeit.timeit('r(-1)', setup='import collections; q = collections.deque([0,1,2,3,4,5,6,7,8]); r=q.rotate', number=1000000)
    0.13374090194702148
    >>> timeit.timeit('r(-1)', setup='import collections; q = collections.deque([0,1,2,3,4,5,6,7,8]); r=q.rotate', number=100000000)
    11.435136079788208
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i want to have a foreach loop where the initial array is changed inside
I have an array where I want to get the next value. $sorting =
I have an array of ~1200 ruby objects and I want to loop over
I have following array construction: $array[$certain_key][some_text_value] And in a while loop, I want to
I have an array I want to check the the last digits if it
I have an array of strings: @array I want to concatenate all strings beginning
i have an array and i want to convert this array in comma seprated
I have an array which I want to use in a query: Array (
I have to array and I want to merge this two array with a
I have an array that I want to sort based on the value of

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.