Add items into Tuples within a List
I have a list that looks as such:
final_list = (1, "Baseline")
final_list2 = (1, "Baseline")
final = [final_list, final_list2]*7
values = [1.0, None, False, True, 1, 100, 'ovr', 1, 'l2', None, 'liblinear', 0.0001, 0, False]
I would like to insert each element from values into the corresponding element tuple in final
. The final result would look like this:
[(1, 'Baseline', 1.0), (1, 'Baseline', None), (1, 'Baseline', False), (1, 'Baseline', True), (1, 'Baseline', 1), (1, 'Baseline', 100), (1, 'Baseline', 'ovr'), (1, 'Baseline', 1), (1, 'Baseline', l2), (1, 'Baseline', None), (1, 'Baseline', 'liblinear'), (1, 'Baseline', 0.0001), (1, 'Baseline',0), (1, 'Baseline', False)]
How can I do this?
python python-3.x
add a comment |
I have a list that looks as such:
final_list = (1, "Baseline")
final_list2 = (1, "Baseline")
final = [final_list, final_list2]*7
values = [1.0, None, False, True, 1, 100, 'ovr', 1, 'l2', None, 'liblinear', 0.0001, 0, False]
I would like to insert each element from values into the corresponding element tuple in final
. The final result would look like this:
[(1, 'Baseline', 1.0), (1, 'Baseline', None), (1, 'Baseline', False), (1, 'Baseline', True), (1, 'Baseline', 1), (1, 'Baseline', 100), (1, 'Baseline', 'ovr'), (1, 'Baseline', 1), (1, 'Baseline', l2), (1, 'Baseline', None), (1, 'Baseline', 'liblinear'), (1, 'Baseline', 0.0001), (1, 'Baseline',0), (1, 'Baseline', False)]
How can I do this?
python python-3.x
2
What have you tried so far?
– natn2323
Nov 19 '18 at 16:38
3
Tuples are immutable. You cannot insert a value into an existing tuple; you need to create new tuples.
– chepner
Nov 19 '18 at 16:40
add a comment |
I have a list that looks as such:
final_list = (1, "Baseline")
final_list2 = (1, "Baseline")
final = [final_list, final_list2]*7
values = [1.0, None, False, True, 1, 100, 'ovr', 1, 'l2', None, 'liblinear', 0.0001, 0, False]
I would like to insert each element from values into the corresponding element tuple in final
. The final result would look like this:
[(1, 'Baseline', 1.0), (1, 'Baseline', None), (1, 'Baseline', False), (1, 'Baseline', True), (1, 'Baseline', 1), (1, 'Baseline', 100), (1, 'Baseline', 'ovr'), (1, 'Baseline', 1), (1, 'Baseline', l2), (1, 'Baseline', None), (1, 'Baseline', 'liblinear'), (1, 'Baseline', 0.0001), (1, 'Baseline',0), (1, 'Baseline', False)]
How can I do this?
python python-3.x
I have a list that looks as such:
final_list = (1, "Baseline")
final_list2 = (1, "Baseline")
final = [final_list, final_list2]*7
values = [1.0, None, False, True, 1, 100, 'ovr', 1, 'l2', None, 'liblinear', 0.0001, 0, False]
I would like to insert each element from values into the corresponding element tuple in final
. The final result would look like this:
[(1, 'Baseline', 1.0), (1, 'Baseline', None), (1, 'Baseline', False), (1, 'Baseline', True), (1, 'Baseline', 1), (1, 'Baseline', 100), (1, 'Baseline', 'ovr'), (1, 'Baseline', 1), (1, 'Baseline', l2), (1, 'Baseline', None), (1, 'Baseline', 'liblinear'), (1, 'Baseline', 0.0001), (1, 'Baseline',0), (1, 'Baseline', False)]
How can I do this?
python python-3.x
python python-3.x
asked Nov 19 '18 at 16:38
Workhorse
439510
439510
2
What have you tried so far?
– natn2323
Nov 19 '18 at 16:38
3
Tuples are immutable. You cannot insert a value into an existing tuple; you need to create new tuples.
– chepner
Nov 19 '18 at 16:40
add a comment |
2
What have you tried so far?
– natn2323
Nov 19 '18 at 16:38
3
Tuples are immutable. You cannot insert a value into an existing tuple; you need to create new tuples.
– chepner
Nov 19 '18 at 16:40
2
2
What have you tried so far?
– natn2323
Nov 19 '18 at 16:38
What have you tried so far?
– natn2323
Nov 19 '18 at 16:38
3
3
Tuples are immutable. You cannot insert a value into an existing tuple; you need to create new tuples.
– chepner
Nov 19 '18 at 16:40
Tuples are immutable. You cannot insert a value into an existing tuple; you need to create new tuples.
– chepner
Nov 19 '18 at 16:40
add a comment |
1 Answer
1
active
oldest
votes
You could do a list-comprehension which iterates through values
appending final_list
at the beginning:
final_list = (1, "Baseline")
values = [1.0, None, False, True, 1, 100, 'ovr', 1, 'l2', None, 'liblinear', 0.0001, 0, False]
print([final_list + (x, ) for x in values])
# [(1, 'Baseline', 1.0),
# (1, 'Baseline', None),
# (1, 'Baseline', False),
# (1, 'Baseline', True),
# (1, 'Baseline', 1),
# (1, 'Baseline', 100),
# (1, 'Baseline', 'ovr'),
# (1, 'Baseline', 1),
# (1, 'Baseline', 'l2'),
# (1, 'Baseline', None),
# (1, 'Baseline', 'liblinear'),
# (1, 'Baseline', 0.0001),
# (1, 'Baseline', 0),
# (1, 'Baseline', False)]
1
Or, if there is indeed a difference betweenfinal_list
andfinal_list2
, then[fl+(v,) for fl, v in zip(final, values)]
or[fl+(v,) for fl, v in zip(itertools.cycle([final_list, final_list2]), values)]
– tobias_k
Nov 19 '18 at 16:53
There is no difference. I wanted to create a nested list and figured tuples would be the way to go.
– Workhorse
Nov 19 '18 at 18:39
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%2f53379060%2fadd-items-into-tuples-within-a-list%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 could do a list-comprehension which iterates through values
appending final_list
at the beginning:
final_list = (1, "Baseline")
values = [1.0, None, False, True, 1, 100, 'ovr', 1, 'l2', None, 'liblinear', 0.0001, 0, False]
print([final_list + (x, ) for x in values])
# [(1, 'Baseline', 1.0),
# (1, 'Baseline', None),
# (1, 'Baseline', False),
# (1, 'Baseline', True),
# (1, 'Baseline', 1),
# (1, 'Baseline', 100),
# (1, 'Baseline', 'ovr'),
# (1, 'Baseline', 1),
# (1, 'Baseline', 'l2'),
# (1, 'Baseline', None),
# (1, 'Baseline', 'liblinear'),
# (1, 'Baseline', 0.0001),
# (1, 'Baseline', 0),
# (1, 'Baseline', False)]
1
Or, if there is indeed a difference betweenfinal_list
andfinal_list2
, then[fl+(v,) for fl, v in zip(final, values)]
or[fl+(v,) for fl, v in zip(itertools.cycle([final_list, final_list2]), values)]
– tobias_k
Nov 19 '18 at 16:53
There is no difference. I wanted to create a nested list and figured tuples would be the way to go.
– Workhorse
Nov 19 '18 at 18:39
add a comment |
You could do a list-comprehension which iterates through values
appending final_list
at the beginning:
final_list = (1, "Baseline")
values = [1.0, None, False, True, 1, 100, 'ovr', 1, 'l2', None, 'liblinear', 0.0001, 0, False]
print([final_list + (x, ) for x in values])
# [(1, 'Baseline', 1.0),
# (1, 'Baseline', None),
# (1, 'Baseline', False),
# (1, 'Baseline', True),
# (1, 'Baseline', 1),
# (1, 'Baseline', 100),
# (1, 'Baseline', 'ovr'),
# (1, 'Baseline', 1),
# (1, 'Baseline', 'l2'),
# (1, 'Baseline', None),
# (1, 'Baseline', 'liblinear'),
# (1, 'Baseline', 0.0001),
# (1, 'Baseline', 0),
# (1, 'Baseline', False)]
1
Or, if there is indeed a difference betweenfinal_list
andfinal_list2
, then[fl+(v,) for fl, v in zip(final, values)]
or[fl+(v,) for fl, v in zip(itertools.cycle([final_list, final_list2]), values)]
– tobias_k
Nov 19 '18 at 16:53
There is no difference. I wanted to create a nested list and figured tuples would be the way to go.
– Workhorse
Nov 19 '18 at 18:39
add a comment |
You could do a list-comprehension which iterates through values
appending final_list
at the beginning:
final_list = (1, "Baseline")
values = [1.0, None, False, True, 1, 100, 'ovr', 1, 'l2', None, 'liblinear', 0.0001, 0, False]
print([final_list + (x, ) for x in values])
# [(1, 'Baseline', 1.0),
# (1, 'Baseline', None),
# (1, 'Baseline', False),
# (1, 'Baseline', True),
# (1, 'Baseline', 1),
# (1, 'Baseline', 100),
# (1, 'Baseline', 'ovr'),
# (1, 'Baseline', 1),
# (1, 'Baseline', 'l2'),
# (1, 'Baseline', None),
# (1, 'Baseline', 'liblinear'),
# (1, 'Baseline', 0.0001),
# (1, 'Baseline', 0),
# (1, 'Baseline', False)]
You could do a list-comprehension which iterates through values
appending final_list
at the beginning:
final_list = (1, "Baseline")
values = [1.0, None, False, True, 1, 100, 'ovr', 1, 'l2', None, 'liblinear', 0.0001, 0, False]
print([final_list + (x, ) for x in values])
# [(1, 'Baseline', 1.0),
# (1, 'Baseline', None),
# (1, 'Baseline', False),
# (1, 'Baseline', True),
# (1, 'Baseline', 1),
# (1, 'Baseline', 100),
# (1, 'Baseline', 'ovr'),
# (1, 'Baseline', 1),
# (1, 'Baseline', 'l2'),
# (1, 'Baseline', None),
# (1, 'Baseline', 'liblinear'),
# (1, 'Baseline', 0.0001),
# (1, 'Baseline', 0),
# (1, 'Baseline', False)]
answered Nov 19 '18 at 16:41
Austin
9,5823828
9,5823828
1
Or, if there is indeed a difference betweenfinal_list
andfinal_list2
, then[fl+(v,) for fl, v in zip(final, values)]
or[fl+(v,) for fl, v in zip(itertools.cycle([final_list, final_list2]), values)]
– tobias_k
Nov 19 '18 at 16:53
There is no difference. I wanted to create a nested list and figured tuples would be the way to go.
– Workhorse
Nov 19 '18 at 18:39
add a comment |
1
Or, if there is indeed a difference betweenfinal_list
andfinal_list2
, then[fl+(v,) for fl, v in zip(final, values)]
or[fl+(v,) for fl, v in zip(itertools.cycle([final_list, final_list2]), values)]
– tobias_k
Nov 19 '18 at 16:53
There is no difference. I wanted to create a nested list and figured tuples would be the way to go.
– Workhorse
Nov 19 '18 at 18:39
1
1
Or, if there is indeed a difference between
final_list
and final_list2
, then [fl+(v,) for fl, v in zip(final, values)]
or [fl+(v,) for fl, v in zip(itertools.cycle([final_list, final_list2]), values)]
– tobias_k
Nov 19 '18 at 16:53
Or, if there is indeed a difference between
final_list
and final_list2
, then [fl+(v,) for fl, v in zip(final, values)]
or [fl+(v,) for fl, v in zip(itertools.cycle([final_list, final_list2]), values)]
– tobias_k
Nov 19 '18 at 16:53
There is no difference. I wanted to create a nested list and figured tuples would be the way to go.
– Workhorse
Nov 19 '18 at 18:39
There is no difference. I wanted to create a nested list and figured tuples would be the way to go.
– Workhorse
Nov 19 '18 at 18:39
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53379060%2fadd-items-into-tuples-within-a-list%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
2
What have you tried so far?
– natn2323
Nov 19 '18 at 16:38
3
Tuples are immutable. You cannot insert a value into an existing tuple; you need to create new tuples.
– chepner
Nov 19 '18 at 16:40