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

The Archive Base Latest Questions

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

Consider the following Python3 program: a = [0, 0] i = 0 a[i] =

  • 0

Consider the following Python3 program:

a = [0, 0]  
i = 0  
a[i] = i = 1  
print(a, i)  

a = [0, 0]  
i = 0  
i = a[i] = 1  
print(a, i)  

I expected the output to be:

[0, 1] 1
[1, 0] 1

But instead I got:

[1, 0] 1
[0, 1] 1

My question is: is there anything in the Python language specification about associativity of the assignment operator, or is the behavior for the above example undefined?

All I was able to find is that expressions are evaluated form left to right, except that r-value is evaluated first in case of assignment, but that doesn’t 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:24:10+00:00Added an answer on May 26, 2026 at 2:24 am

    Short answer: the code is well defined; the order is left-to-right.

    Long answer:

    First of all, let’s get the terminology right. Unlike in some other languages, assignment in Python is a statement, not an operator. This means that you can’t use assignment as part of another expression: for example i = (j = 0) is not valid Python code.

    The assignment statement is defined to explicitly permit multiple assignment targets (in your example, these are i and a[i]). Each target can be a list, but let’s leave that aside.

    Where there are multiple assignment targets, the value is assigned from left to right. To quote the documentation:

    An assignment statement evaluates the expression list (remember that
    this can be a single expression or a comma-separated list, the latter
    yielding a tuple) and assigns the single resulting object to each of
    the target lists, from left to right.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the two following Python code examples, which achieves the same but with significant
Consider the following function, which does not work in Python, but I will use
Consider following program: static void Main (string[] args) { int i; uint ui; i
consider the following python code: import gtk class MainWindow(): def __init__(self): self.window = gtk.Window()
Consider the following birthdays (as dob ): 1-Jun-68 1-Jun-69 When parsed with Python’s datetime.strptime(dob,
Consider the following Python exception: [...] f.extractall() File C:\Python26\lib\zipfile.py, line 935, in extractall self.extract(zipinfo,
Consider the following Python (3.x) code: class Foo(object): def bar(self): pass foo = Foo()
consider the following python code class Base(object): def __init__(self): self.foo = 5 class Derived(Base):
Consider the following Python code: 30 url = http://www.google.com/search?hl=en&safe=off&q=Monkey 31 url_object = urllib.request.urlopen(url); 32
Let's consider the following table models for sqlalchemy in python. class Worker(Base): id Column(Integer,

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.