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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T23:28:23+00:00 2026-06-12T23:28:23+00:00

In python you can change a list like this: In [303]: x = [1,2,3,4,5,6]

  • 0

In python you can change a list like this:

In [303]: x = [1,2,3,4,5,6]

In [304]: x[x <= 3]+=3

In [305]: x
Out[306]: [4, 2, 3, 4, 5, 6]

I have known about this for some time now, but I don’t think I fully understand whats going on behind the scenes. I would appriciate, if someone would find the time to explain.

In [307]: x = [1,2,3,4,5,6]

In [308]: dis.dis('x[x <= 3]+=3')
          0 SETUP_LOOP      30811 (to 30814)
          3 SLICE+2        
          4 STORE_SUBSCR   
          5 DELETE_SUBSCR  
          6 SLICE+2        
          7 DELETE_SLICE+1 
          8 FOR_ITER        15659 (to 15670)
         11 DELETE_SLICE+1 

In [309]: x
Out[309]: [1, 2, 3, 4, 5, 6]

In [310]: x[x <= 3]+=3

In [311]: x
Out[311]: [4, 2, 3, 4, 5, 6]

In [312]: x<=3
Out[312]: False

In [313]: x[False]+=3

In [314]: x
Out[314]: [7, 2, 3, 4, 5, 6]
  • 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-12T23:28:25+00:00Added an answer on June 12, 2026 at 11:28 pm

    x <= 3 is a boolean expression. Since in Python, the boolean type is a subclass of int, the False outcome is interpreted as 0, so the end effect is:

    x[0] += 3
    

    Or, demonstrated in a different way:

    >>> False == 0
    True
    >>> True == 1
    True
    >>> isinstance(False, int)
    True
    

    The dis.dis() method doesn’t work with strings; it works with code objects (or something that contains code, such as a function, method, class, or module), or with bytecode. The fact that it seems to be able to decode your string is a happy coincidence; SETUP_LOOP is opcode 120 (the ASCII value for x), the whole string is being interpreted as a set of opcodes and offsets.

    Use a function instead:

    >>> def foo(): x[x <= 3]+=3
    ... 
    >>> dis.dis(foo)
      1           0 LOAD_GLOBAL              0 (x)
                  3 LOAD_GLOBAL              0 (x)
                  6 LOAD_CONST               1 (3)
                  9 COMPARE_OP               1 (<=)
                 12 DUP_TOPX                 2
                 15 BINARY_SUBSCR       
                 16 LOAD_CONST               1 (3)
                 19 INPLACE_ADD         
                 20 ROT_THREE           
                 21 STORE_SUBSCR        
                 22 LOAD_CONST               0 (None)
                 25 RETURN_VALUE        
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Very new to python and can't understand why this isn't working. I have a
I am writing some code in Python something like this: import sys try: for
I'd like to change the behavior of Python's list displays so that instead of
In Python I can define a function as follows: def func(kw1=None,kw2=None,**kwargs): ... In this
In python how can i develop this algorithm to find common patterns in a
Im still new to python. I have a text file with a list of
I'd like to store some information about a class as class (static) variables. However,
I usually perform things like this in C++, but I'm using python to write
I've looked at a number of questions but still can't quite figure this out.
I have a Python 3.2.2 program and within it is a list that is

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.