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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:34:07+00:00 2026-05-27T07:34:07+00:00

Given a list, e.g. x = [True]*20 , I want to assign False to

  • 0

Given a list, e.g. x = [True]*20, I want to assign False to every other element.

x[::2] = False

raises TypeError: must assign iterable to extended slice

So I naively assumed you could do something like this:

x[::2] = itertools.repeat(False)

or

x[::2] = itertools.cycle([False])

However, as far as I can tell, this results in an infinite loop. Why is there an infinite loop? Is there an alternative approach that does not involve knowing the number of elements in the slice before assignment?

EDIT: I understand that x[::2] = [False] * len(x)/2 works in this case, or you can come up with an expression for the multiplier on the right side in the more general case. I’m trying to understand what causes itertools to cycle indefinitely and why list assignment behaves differently from numpy array assignment. I think there must be something fundamental about python I’m misunderstanding. I was also thinking originally there might be performance reasons to prefer itertools to list comprehension or creating another n-element list.

  • 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-27T07:34:08+00:00Added an answer on May 27, 2026 at 7:34 am

    As Mark Tolonen pointed out in a concise comment, the reason why your itertools attempts are cycling indefinitely is because, for the list assignment, python is checking the length of the right hand side.

    Now to really dig in…

    When you say:

    x[::2] = itertools.repeat(False)
    

    The left hand side (x[::2]) is a list, and you are assigning a value to a list where the value is the itertools.repeat(False) iterable, which will iterate forever since it wasn’t given a length (as per the docs).

    If you dig into the list assignment code in the cPython implementation, you’ll find the unfortunately/painfully named function list_ass_slice, which is at the root of a lot of list assignment stuff. In that code you’ll see this segment:

    v_as_SF = PySequence_Fast(v, "can only assign an iterable");
    if(v_as_SF == NULL)
        goto Error;
    n = PySequence_Fast_GET_SIZE(v_as_SF);
    

    Here it is trying to get the length (n) of the iterable you are assigning to the list. However, before even getting there it is getting stuck on PySequence_Fast, where it ends up trying to convert your iterable to a list (with PySequence_List), within which it ultimately creates an empty list and tries to simply extend it with your iterable.

    To extend the list with the iterable, it uses listextend(), and in there you’ll see the root of the problem:

    /* Run iterator to exhaustion. */
    for (;;) {
    

    and there you go.

    Or least I think so… 🙂 It was an interesting question so I thought I’d have some fun and dig through the source to see what was up, and ended up there.

    As to the different behaviour with numpy arrays, it will simply be a difference in how the numpy.array assignments are handled.

    Note that using itertools.repeat doesn’t work in numpy, but it doesn’t hang up (I didn’t check the implementation to figure out why):

    >>> import numpy, itertools
    >>> x = numpy.ones(10,dtype='bool')
    >>> x[::2] = itertools.repeat(False)
    >>> x
    array([ True,  True,  True,  True,  True,  True,  True,  True,  True,  True], dtype=bool)
    >>> #but the scalar assignment does work as advertised...
    >>> x = numpy.ones(10,dtype='bool')
    >>> x[::2] = False
    >>> x
    array([False,  True, False,  True, False,  True, False,  True, False,  True], dtype=bool)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For example, given the list ['one', 'two', 'one'] , the algorithm should return True
Given a list, how would I select a new list, containing a slice of
Given a list of strings, I want to sort it alphabetically and remove duplicates.
I want to test (true or false) whether an arbitrary XML file matches a
I have a List<T> that I want to search not for a given item
Given a list of items, I want to create a function to check if
Suppose we want those elements of list x for which the corresponding element of
Given a list of locations such as <td>El Cerrito, CA</td> <td>Corvallis, OR</td> <td>Morganton, NC</td>
Given a list ["foo", "bar", "baz"] and an item in the list "bar" ,
Given a list of urls, I would like to check that each url: Returns

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.