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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:54:44+00:00 2026-05-26T02:54:44+00:00

I want to build some lists (a, b, c, d) by extending periodically those

  • 0

I want to build some lists (a, b, c, d) by extending periodically those given in input.

The structure of the periodicity is such that the first element of each list must not be repeated, while all the other elements must be until the maximum length set in input is reached (it is possible that the period does not get to be repeated an integer number of times).

To give an example, if I have as input

a = [1, 2, 3, 4]          max_len=11

I want to obtain as output

a = [1, 2, 3, 4, 2, 3, 4, 2, 3, 4, 2]

I wrote this piece of code:

for mylist in [a, b, c d]:   
    period = mylist[1:]     # all elements but the first are repeated
    while len(mylist)< max_len: 
        mylist.extend(period)
    mylist = mylist[:max_len]   # cut to max_len                        
    print mylist
print a, b, c, d

If I run this, I see from the two print commands that my lists are how I want them to be while the program is still in the loop, but they get back to the “non-sliced” length when out of the loop, that is, a gets back to the length greater than max_len where the period is repeated exactly 4 times:

 a = [1, 2, 3, 4, 2, 3, 4, 2, 3, 4, 2, 3, 4]

Why is that? It looks like the program has forgotten the slicing (but not the extension).

I understand that it is a pretty simple problem, and I could get around it in some other way, but I would like to understand why this idea does not work.
Thank you for your help!

  • 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-26T02:54:45+00:00Added an answer on May 26, 2026 at 2:54 am

    The problem is that mylist = mylist[:max_len] creates a new list instead of modifying mylist in place, so the results never propagate back to a et al.

    If you replace:

    mylist = mylist[:max_len]
    

    with

    mylist[:] = mylist[:max_len]
    

    that’ll fix it.

    As an aside, you could use itertools for the transformation:

    In [10]: [a[0]] + [v for v in itertools.islice(itertools.cycle(a[1:]), max_len - 1)]
    Out[10]: [1, 2, 3, 4, 2, 3, 4, 2, 3, 4, 2]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to build a TreeModel from some List s that contain the source
So I want to build some automated guidance for our architecture. I found the
Basically I want to build a website based on some original content, the content
I want to build a workspace with subversion, and I followed some tutorials; Install
I want to build a security related application. I would be storing some important
I want build a sketch pad app on iPhone, I assume that this type
So I'm making a website that allows users to build contact lists. So their
I have some 5 application running in my tomcat server. I want to build
I want build a financial analysis tool that will feature screen after screen of
I want to build groups that users can join, The flow of steps and

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.