Strange Effect of Python Assignment?
As we all know, for Python, we could easily assignment multiple variables in one line. But here I encounter a strange situation. Say we have a list:
x = [1, 2, 3, 4]
And then, we do
x[0], x[x[0]] = 2, 1
Finally, we would get
x = [2, 2, 3, 4]
instead of
x = [2, 1, 3, 4]
Could anyone explain what is going wrong here? How would python implement the multiple variables assignment in one line?
Thanks in advance.
python syntax programming-languages
add a comment |
As we all know, for Python, we could easily assignment multiple variables in one line. But here I encounter a strange situation. Say we have a list:
x = [1, 2, 3, 4]
And then, we do
x[0], x[x[0]] = 2, 1
Finally, we would get
x = [2, 2, 3, 4]
instead of
x = [2, 1, 3, 4]
Could anyone explain what is going wrong here? How would python implement the multiple variables assignment in one line?
Thanks in advance.
python syntax programming-languages
Please also check this: stackoverflow.com/questions/8725673/…
– supl
Nov 22 '18 at 5:34
So basically, Python will evaluate right-hand side of the statement (only read, no contention), and then it will assign to variables on the left-hand side from left to right. Thanks all!!
– UserNotFound
Nov 22 '18 at 5:42
add a comment |
As we all know, for Python, we could easily assignment multiple variables in one line. But here I encounter a strange situation. Say we have a list:
x = [1, 2, 3, 4]
And then, we do
x[0], x[x[0]] = 2, 1
Finally, we would get
x = [2, 2, 3, 4]
instead of
x = [2, 1, 3, 4]
Could anyone explain what is going wrong here? How would python implement the multiple variables assignment in one line?
Thanks in advance.
python syntax programming-languages
As we all know, for Python, we could easily assignment multiple variables in one line. But here I encounter a strange situation. Say we have a list:
x = [1, 2, 3, 4]
And then, we do
x[0], x[x[0]] = 2, 1
Finally, we would get
x = [2, 2, 3, 4]
instead of
x = [2, 1, 3, 4]
Could anyone explain what is going wrong here? How would python implement the multiple variables assignment in one line?
Thanks in advance.
python syntax programming-languages
python syntax programming-languages
asked Nov 22 '18 at 5:21
UserNotFoundUserNotFound
11
11
Please also check this: stackoverflow.com/questions/8725673/…
– supl
Nov 22 '18 at 5:34
So basically, Python will evaluate right-hand side of the statement (only read, no contention), and then it will assign to variables on the left-hand side from left to right. Thanks all!!
– UserNotFound
Nov 22 '18 at 5:42
add a comment |
Please also check this: stackoverflow.com/questions/8725673/…
– supl
Nov 22 '18 at 5:34
So basically, Python will evaluate right-hand side of the statement (only read, no contention), and then it will assign to variables on the left-hand side from left to right. Thanks all!!
– UserNotFound
Nov 22 '18 at 5:42
Please also check this: stackoverflow.com/questions/8725673/…
– supl
Nov 22 '18 at 5:34
Please also check this: stackoverflow.com/questions/8725673/…
– supl
Nov 22 '18 at 5:34
So basically, Python will evaluate right-hand side of the statement (only read, no contention), and then it will assign to variables on the left-hand side from left to right. Thanks all!!
– UserNotFound
Nov 22 '18 at 5:42
So basically, Python will evaluate right-hand side of the statement (only read, no contention), and then it will assign to variables on the left-hand side from left to right. Thanks all!!
– UserNotFound
Nov 22 '18 at 5:42
add a comment |
2 Answers
2
active
oldest
votes
The resulting list is not [2, 2, 3, 4]
, it's [2, 2, 1, 4]
. x[0]
is assigned the value 2
, then x[x[0]]
becomes x[2]
and is assigned the value 1
yes. It is [2, 2, 1, 4], Thanks!
– UserNotFound
Nov 22 '18 at 5:42
add a comment |
What happens here is, python first executes your first instruction . x[0]= 2
So after 1st execution, x=[2,2,3,4]
Then it executes the 2nd one. So it changes the value of x[2]
after 2nd execution, x=[2,2,1,4]
and gives the result as [2, 2, 1, 4]
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53424334%2fstrange-effect-of-python-assignment%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
The resulting list is not [2, 2, 3, 4]
, it's [2, 2, 1, 4]
. x[0]
is assigned the value 2
, then x[x[0]]
becomes x[2]
and is assigned the value 1
yes. It is [2, 2, 1, 4], Thanks!
– UserNotFound
Nov 22 '18 at 5:42
add a comment |
The resulting list is not [2, 2, 3, 4]
, it's [2, 2, 1, 4]
. x[0]
is assigned the value 2
, then x[x[0]]
becomes x[2]
and is assigned the value 1
yes. It is [2, 2, 1, 4], Thanks!
– UserNotFound
Nov 22 '18 at 5:42
add a comment |
The resulting list is not [2, 2, 3, 4]
, it's [2, 2, 1, 4]
. x[0]
is assigned the value 2
, then x[x[0]]
becomes x[2]
and is assigned the value 1
The resulting list is not [2, 2, 3, 4]
, it's [2, 2, 1, 4]
. x[0]
is assigned the value 2
, then x[x[0]]
becomes x[2]
and is assigned the value 1
answered Nov 22 '18 at 5:29
M.GM.G
388310
388310
yes. It is [2, 2, 1, 4], Thanks!
– UserNotFound
Nov 22 '18 at 5:42
add a comment |
yes. It is [2, 2, 1, 4], Thanks!
– UserNotFound
Nov 22 '18 at 5:42
yes. It is [2, 2, 1, 4], Thanks!
– UserNotFound
Nov 22 '18 at 5:42
yes. It is [2, 2, 1, 4], Thanks!
– UserNotFound
Nov 22 '18 at 5:42
add a comment |
What happens here is, python first executes your first instruction . x[0]= 2
So after 1st execution, x=[2,2,3,4]
Then it executes the 2nd one. So it changes the value of x[2]
after 2nd execution, x=[2,2,1,4]
and gives the result as [2, 2, 1, 4]
add a comment |
What happens here is, python first executes your first instruction . x[0]= 2
So after 1st execution, x=[2,2,3,4]
Then it executes the 2nd one. So it changes the value of x[2]
after 2nd execution, x=[2,2,1,4]
and gives the result as [2, 2, 1, 4]
add a comment |
What happens here is, python first executes your first instruction . x[0]= 2
So after 1st execution, x=[2,2,3,4]
Then it executes the 2nd one. So it changes the value of x[2]
after 2nd execution, x=[2,2,1,4]
and gives the result as [2, 2, 1, 4]
What happens here is, python first executes your first instruction . x[0]= 2
So after 1st execution, x=[2,2,3,4]
Then it executes the 2nd one. So it changes the value of x[2]
after 2nd execution, x=[2,2,1,4]
and gives the result as [2, 2, 1, 4]
answered Nov 22 '18 at 5:30
Sandesh34Sandesh34
254112
254112
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53424334%2fstrange-effect-of-python-assignment%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Please also check this: stackoverflow.com/questions/8725673/…
– supl
Nov 22 '18 at 5:34
So basically, Python will evaluate right-hand side of the statement (only read, no contention), and then it will assign to variables on the left-hand side from left to right. Thanks all!!
– UserNotFound
Nov 22 '18 at 5:42