How to modify last element of Python list in list comprehension?
I have a code segment like following which throws an invalid syntax error:
lst = [0, 0, 0]
new_lst = [lst[-1] = i + 1 for i in range(3)]
Basically, I want new_lst
to be list of lists, where every list will be lst
but the last element changed. Any ideas how to do this in Python?
python list
add a comment |
I have a code segment like following which throws an invalid syntax error:
lst = [0, 0, 0]
new_lst = [lst[-1] = i + 1 for i in range(3)]
Basically, I want new_lst
to be list of lists, where every list will be lst
but the last element changed. Any ideas how to do this in Python?
python list
1
Assignments are not allowed inside of list comprehensions.
– Klaus D.
Aug 20 '18 at 11:10
add a comment |
I have a code segment like following which throws an invalid syntax error:
lst = [0, 0, 0]
new_lst = [lst[-1] = i + 1 for i in range(3)]
Basically, I want new_lst
to be list of lists, where every list will be lst
but the last element changed. Any ideas how to do this in Python?
python list
I have a code segment like following which throws an invalid syntax error:
lst = [0, 0, 0]
new_lst = [lst[-1] = i + 1 for i in range(3)]
Basically, I want new_lst
to be list of lists, where every list will be lst
but the last element changed. Any ideas how to do this in Python?
python list
python list
edited Nov 14 '18 at 11:51
aydow
2,36011026
2,36011026
asked Aug 20 '18 at 11:09
terettterett
22617
22617
1
Assignments are not allowed inside of list comprehensions.
– Klaus D.
Aug 20 '18 at 11:10
add a comment |
1
Assignments are not allowed inside of list comprehensions.
– Klaus D.
Aug 20 '18 at 11:10
1
1
Assignments are not allowed inside of list comprehensions.
– Klaus D.
Aug 20 '18 at 11:10
Assignments are not allowed inside of list comprehensions.
– Klaus D.
Aug 20 '18 at 11:10
add a comment |
1 Answer
1
active
oldest
votes
You can only make assignments when they are stand-alone statements I.e. not within a list comprehension. Other examples of this would be in control statements like if
s or for
s
In [945]: lst = [0, 0, 0]
In [946]: new_lst = [lst[:-1] + [i + 1] for i in range(3)]
In [947]: new_lst
Out[947]: [[0, 0, 1], [0, 0, 2], [0, 0, 3]]
lst[:-1]
takes every element from lst
except for the last and [i + 1]
will be appended to the end of the list
Thank you, this works.
– terett
Aug 20 '18 at 11:12
@terett if it works and has helped you please upvote and accept the answer. thanks!
– aydow
Aug 27 '18 at 21:17
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%2f51929478%2fhow-to-modify-last-element-of-python-list-in-list-comprehension%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can only make assignments when they are stand-alone statements I.e. not within a list comprehension. Other examples of this would be in control statements like if
s or for
s
In [945]: lst = [0, 0, 0]
In [946]: new_lst = [lst[:-1] + [i + 1] for i in range(3)]
In [947]: new_lst
Out[947]: [[0, 0, 1], [0, 0, 2], [0, 0, 3]]
lst[:-1]
takes every element from lst
except for the last and [i + 1]
will be appended to the end of the list
Thank you, this works.
– terett
Aug 20 '18 at 11:12
@terett if it works and has helped you please upvote and accept the answer. thanks!
– aydow
Aug 27 '18 at 21:17
add a comment |
You can only make assignments when they are stand-alone statements I.e. not within a list comprehension. Other examples of this would be in control statements like if
s or for
s
In [945]: lst = [0, 0, 0]
In [946]: new_lst = [lst[:-1] + [i + 1] for i in range(3)]
In [947]: new_lst
Out[947]: [[0, 0, 1], [0, 0, 2], [0, 0, 3]]
lst[:-1]
takes every element from lst
except for the last and [i + 1]
will be appended to the end of the list
Thank you, this works.
– terett
Aug 20 '18 at 11:12
@terett if it works and has helped you please upvote and accept the answer. thanks!
– aydow
Aug 27 '18 at 21:17
add a comment |
You can only make assignments when they are stand-alone statements I.e. not within a list comprehension. Other examples of this would be in control statements like if
s or for
s
In [945]: lst = [0, 0, 0]
In [946]: new_lst = [lst[:-1] + [i + 1] for i in range(3)]
In [947]: new_lst
Out[947]: [[0, 0, 1], [0, 0, 2], [0, 0, 3]]
lst[:-1]
takes every element from lst
except for the last and [i + 1]
will be appended to the end of the list
You can only make assignments when they are stand-alone statements I.e. not within a list comprehension. Other examples of this would be in control statements like if
s or for
s
In [945]: lst = [0, 0, 0]
In [946]: new_lst = [lst[:-1] + [i + 1] for i in range(3)]
In [947]: new_lst
Out[947]: [[0, 0, 1], [0, 0, 2], [0, 0, 3]]
lst[:-1]
takes every element from lst
except for the last and [i + 1]
will be appended to the end of the list
edited Aug 20 '18 at 11:43
answered Aug 20 '18 at 11:11
aydowaydow
2,36011026
2,36011026
Thank you, this works.
– terett
Aug 20 '18 at 11:12
@terett if it works and has helped you please upvote and accept the answer. thanks!
– aydow
Aug 27 '18 at 21:17
add a comment |
Thank you, this works.
– terett
Aug 20 '18 at 11:12
@terett if it works and has helped you please upvote and accept the answer. thanks!
– aydow
Aug 27 '18 at 21:17
Thank you, this works.
– terett
Aug 20 '18 at 11:12
Thank you, this works.
– terett
Aug 20 '18 at 11:12
@terett if it works and has helped you please upvote and accept the answer. thanks!
– aydow
Aug 27 '18 at 21:17
@terett if it works and has helped you please upvote and accept the answer. thanks!
– aydow
Aug 27 '18 at 21:17
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%2f51929478%2fhow-to-modify-last-element-of-python-list-in-list-comprehension%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
1
Assignments are not allowed inside of list comprehensions.
– Klaus D.
Aug 20 '18 at 11:10