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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:45:18+00:00 2026-06-12T00:45:18+00:00

I’m writing a parser, and in the process of debugging it, I found that

  • 0

I’m writing a parser, and in the process of debugging it, I found that apparently, this is legal Python:

for []    in [[]]: print 0

and so is this (!):

for [][:] in [[]]: print 0

I don’t blame the parser for getting confused… I’m having trouble figuring out how to interpret it!

What exactly does this statement mean?

  • 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-12T00:45:19+00:00Added an answer on June 12, 2026 at 12:45 am

    In terms of execution: nothing.

    The for loop itself loops over an empty list, so no iterations will take place.

    And that’s a good thing, because the for [] means: assign each entry in the loop to 0 variables. The latter part is probably what is puzzling you.

    The statement is legal because the target token token_list allows you to assign the values in a sequence to an equally large sequence of variable names; we call this tuple unpacking. The following are more useful examples of target lists, in assignment and deletion:

    (a, b, c) = range(3)
    del a, b, c
    

    You can do the same in a for loop:

    nested = [[1,2,3], [4,5,6]]
    for a, b, c in nested:
        print nested
    

    You can use both tuples and lists for the target_list token, this is legal too:

    [a, b] = (1, 2)
    

    However, in Python, a list can be empty. Thus, the following is legal, however non-sensical:

    [] = []
    

    and finally, so is this:

    nested_empty = [[], [], []]
    for [] in nested_empty:
        pass
    

    More fun with target lists:

    [][:] = [1, 2, 3]
    

    Now the left-hand side is using a slice in the assignment. From the documentation:

    If the target is a slicing: The primary expression in the reference is evaluated. It should yield a mutable sequence object (such as a list). The assigned object should be a sequence object of the same type. Next, the lower and upper bound expressions are evaluated, insofar they are present; defaults are zero and the sequence’s length. The bounds should evaluate to (small) integers. If either bound is negative, the sequence’s length is added to it. The resulting bounds are clipped to lie between zero and the sequence’s length, inclusive. Finally, the sequence object is asked to replace the slice with the items of the assigned sequence. The length of the slice may be different from the length of the assigned sequence, thus changing the length of the target sequence, if the object allows it.

    So here we are no longer using tuple unpacking; instead we replace a section of the left-hand list with the right-hand list. But because in our example the left-hand list is an anonymous list literal, the resulting altered list is lost again.

    But because such an assignment is also legal in a for loop, the following is legal syntax, albeit rather non-sensical:

    for [][:] in [range(i) for i in range(10)]: print 0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I know there's a lot of other questions out there that deal with this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I have this code to decode numeric html entities to the UTF8 equivalent character.

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.