update dict value from one to another based on condition python
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
Is there any better way to write following code? I have two dict with same set of data as key, I want to iterate dict_a and check if any key with only one value, then update the value to dict_b. I have following working code but it seem there should be a better way to do it
dict_a = {
'first': {1,2},
'second': {2, 7, 10, 22},
'third': {3},
'fourth': {6,8},
'fifth': {1},
}
dict_b = {
'first': 11,
'second': 4,
'third': 1,
'fourth': 1000,
'fifth': 8
}
for k, v in dict_a.items():
if len(v) == 1:
dict_b[k] = v.pop()
#=>
#dict_b = {
#'first': 11,
#'second': 4,
#'third': 3,
#'fourth': 1000,
#'fifth': 1
#}
python python-3.x dictionary set
add a comment |
Is there any better way to write following code? I have two dict with same set of data as key, I want to iterate dict_a and check if any key with only one value, then update the value to dict_b. I have following working code but it seem there should be a better way to do it
dict_a = {
'first': {1,2},
'second': {2, 7, 10, 22},
'third': {3},
'fourth': {6,8},
'fifth': {1},
}
dict_b = {
'first': 11,
'second': 4,
'third': 1,
'fourth': 1000,
'fifth': 8
}
for k, v in dict_a.items():
if len(v) == 1:
dict_b[k] = v.pop()
#=>
#dict_b = {
#'first': 11,
#'second': 4,
#'third': 3,
#'fourth': 1000,
#'fifth': 1
#}
python python-3.x dictionary set
2
This will modify the values indict_aas well. Is that what you want?
– schwobaseggl
Nov 23 '18 at 16:12
yes, i do not really care about dict_a actually
– jacobcan118
Nov 23 '18 at 16:14
add a comment |
Is there any better way to write following code? I have two dict with same set of data as key, I want to iterate dict_a and check if any key with only one value, then update the value to dict_b. I have following working code but it seem there should be a better way to do it
dict_a = {
'first': {1,2},
'second': {2, 7, 10, 22},
'third': {3},
'fourth': {6,8},
'fifth': {1},
}
dict_b = {
'first': 11,
'second': 4,
'third': 1,
'fourth': 1000,
'fifth': 8
}
for k, v in dict_a.items():
if len(v) == 1:
dict_b[k] = v.pop()
#=>
#dict_b = {
#'first': 11,
#'second': 4,
#'third': 3,
#'fourth': 1000,
#'fifth': 1
#}
python python-3.x dictionary set
Is there any better way to write following code? I have two dict with same set of data as key, I want to iterate dict_a and check if any key with only one value, then update the value to dict_b. I have following working code but it seem there should be a better way to do it
dict_a = {
'first': {1,2},
'second': {2, 7, 10, 22},
'third': {3},
'fourth': {6,8},
'fifth': {1},
}
dict_b = {
'first': 11,
'second': 4,
'third': 1,
'fourth': 1000,
'fifth': 8
}
for k, v in dict_a.items():
if len(v) == 1:
dict_b[k] = v.pop()
#=>
#dict_b = {
#'first': 11,
#'second': 4,
#'third': 3,
#'fourth': 1000,
#'fifth': 1
#}
python python-3.x dictionary set
python python-3.x dictionary set
edited Nov 23 '18 at 16:19
jpp
103k2166116
103k2166116
asked Nov 23 '18 at 16:09
jacobcan118jacobcan118
660423
660423
2
This will modify the values indict_aas well. Is that what you want?
– schwobaseggl
Nov 23 '18 at 16:12
yes, i do not really care about dict_a actually
– jacobcan118
Nov 23 '18 at 16:14
add a comment |
2
This will modify the values indict_aas well. Is that what you want?
– schwobaseggl
Nov 23 '18 at 16:12
yes, i do not really care about dict_a actually
– jacobcan118
Nov 23 '18 at 16:14
2
2
This will modify the values in
dict_a as well. Is that what you want?– schwobaseggl
Nov 23 '18 at 16:12
This will modify the values in
dict_a as well. Is that what you want?– schwobaseggl
Nov 23 '18 at 16:12
yes, i do not really care about dict_a actually
– jacobcan118
Nov 23 '18 at 16:14
yes, i do not really care about dict_a actually
– jacobcan118
Nov 23 '18 at 16:14
add a comment |
2 Answers
2
active
oldest
votes
You are, possibly unnecessarily, modifying dict_a when you use pop on set values in dict_a. You can, instead, use next + iter to extract the only value of a set:
dict_b[k] = next(iter(v))
This doesn't appear to result in what the OP wants, but rather pulls the first element from each set. Am I missing something?
– Jonah Bishop
Nov 23 '18 at 16:19
I think so.. if the length of a set is 1, i.e. the unchangedifclause, then pulling the only (as it happens, "first") element from each set is exactly whatv.popachieves.
– jpp
Nov 23 '18 at 16:20
Ah, I forgot to put this within the existing conditional. My mistake. Clever implementation.
– Jonah Bishop
Nov 23 '18 at 16:21
@JonahBishop Also, sets are inherently unordered. First and last have no meaning. Both methods retrieve any element.
– schwobaseggl
Nov 23 '18 at 16:22
add a comment |
You can make it a one-liner using update and a generator:
dict_b.update((k, v.pop()) for k, v in dict_a.items() if len(v) == 1)
Algorithmically, this doesn't gain anything, but will utilize some optimzations that come with the used syntactic means.
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%2f53449871%2fupdate-dict-value-from-one-to-another-based-on-condition-python%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
You are, possibly unnecessarily, modifying dict_a when you use pop on set values in dict_a. You can, instead, use next + iter to extract the only value of a set:
dict_b[k] = next(iter(v))
This doesn't appear to result in what the OP wants, but rather pulls the first element from each set. Am I missing something?
– Jonah Bishop
Nov 23 '18 at 16:19
I think so.. if the length of a set is 1, i.e. the unchangedifclause, then pulling the only (as it happens, "first") element from each set is exactly whatv.popachieves.
– jpp
Nov 23 '18 at 16:20
Ah, I forgot to put this within the existing conditional. My mistake. Clever implementation.
– Jonah Bishop
Nov 23 '18 at 16:21
@JonahBishop Also, sets are inherently unordered. First and last have no meaning. Both methods retrieve any element.
– schwobaseggl
Nov 23 '18 at 16:22
add a comment |
You are, possibly unnecessarily, modifying dict_a when you use pop on set values in dict_a. You can, instead, use next + iter to extract the only value of a set:
dict_b[k] = next(iter(v))
This doesn't appear to result in what the OP wants, but rather pulls the first element from each set. Am I missing something?
– Jonah Bishop
Nov 23 '18 at 16:19
I think so.. if the length of a set is 1, i.e. the unchangedifclause, then pulling the only (as it happens, "first") element from each set is exactly whatv.popachieves.
– jpp
Nov 23 '18 at 16:20
Ah, I forgot to put this within the existing conditional. My mistake. Clever implementation.
– Jonah Bishop
Nov 23 '18 at 16:21
@JonahBishop Also, sets are inherently unordered. First and last have no meaning. Both methods retrieve any element.
– schwobaseggl
Nov 23 '18 at 16:22
add a comment |
You are, possibly unnecessarily, modifying dict_a when you use pop on set values in dict_a. You can, instead, use next + iter to extract the only value of a set:
dict_b[k] = next(iter(v))
You are, possibly unnecessarily, modifying dict_a when you use pop on set values in dict_a. You can, instead, use next + iter to extract the only value of a set:
dict_b[k] = next(iter(v))
answered Nov 23 '18 at 16:14
jppjpp
103k2166116
103k2166116
This doesn't appear to result in what the OP wants, but rather pulls the first element from each set. Am I missing something?
– Jonah Bishop
Nov 23 '18 at 16:19
I think so.. if the length of a set is 1, i.e. the unchangedifclause, then pulling the only (as it happens, "first") element from each set is exactly whatv.popachieves.
– jpp
Nov 23 '18 at 16:20
Ah, I forgot to put this within the existing conditional. My mistake. Clever implementation.
– Jonah Bishop
Nov 23 '18 at 16:21
@JonahBishop Also, sets are inherently unordered. First and last have no meaning. Both methods retrieve any element.
– schwobaseggl
Nov 23 '18 at 16:22
add a comment |
This doesn't appear to result in what the OP wants, but rather pulls the first element from each set. Am I missing something?
– Jonah Bishop
Nov 23 '18 at 16:19
I think so.. if the length of a set is 1, i.e. the unchangedifclause, then pulling the only (as it happens, "first") element from each set is exactly whatv.popachieves.
– jpp
Nov 23 '18 at 16:20
Ah, I forgot to put this within the existing conditional. My mistake. Clever implementation.
– Jonah Bishop
Nov 23 '18 at 16:21
@JonahBishop Also, sets are inherently unordered. First and last have no meaning. Both methods retrieve any element.
– schwobaseggl
Nov 23 '18 at 16:22
This doesn't appear to result in what the OP wants, but rather pulls the first element from each set. Am I missing something?
– Jonah Bishop
Nov 23 '18 at 16:19
This doesn't appear to result in what the OP wants, but rather pulls the first element from each set. Am I missing something?
– Jonah Bishop
Nov 23 '18 at 16:19
I think so.. if the length of a set is 1, i.e. the unchanged
if clause, then pulling the only (as it happens, "first") element from each set is exactly what v.pop achieves.– jpp
Nov 23 '18 at 16:20
I think so.. if the length of a set is 1, i.e. the unchanged
if clause, then pulling the only (as it happens, "first") element from each set is exactly what v.pop achieves.– jpp
Nov 23 '18 at 16:20
Ah, I forgot to put this within the existing conditional. My mistake. Clever implementation.
– Jonah Bishop
Nov 23 '18 at 16:21
Ah, I forgot to put this within the existing conditional. My mistake. Clever implementation.
– Jonah Bishop
Nov 23 '18 at 16:21
@JonahBishop Also, sets are inherently unordered. First and last have no meaning. Both methods retrieve any element.
– schwobaseggl
Nov 23 '18 at 16:22
@JonahBishop Also, sets are inherently unordered. First and last have no meaning. Both methods retrieve any element.
– schwobaseggl
Nov 23 '18 at 16:22
add a comment |
You can make it a one-liner using update and a generator:
dict_b.update((k, v.pop()) for k, v in dict_a.items() if len(v) == 1)
Algorithmically, this doesn't gain anything, but will utilize some optimzations that come with the used syntactic means.
add a comment |
You can make it a one-liner using update and a generator:
dict_b.update((k, v.pop()) for k, v in dict_a.items() if len(v) == 1)
Algorithmically, this doesn't gain anything, but will utilize some optimzations that come with the used syntactic means.
add a comment |
You can make it a one-liner using update and a generator:
dict_b.update((k, v.pop()) for k, v in dict_a.items() if len(v) == 1)
Algorithmically, this doesn't gain anything, but will utilize some optimzations that come with the used syntactic means.
You can make it a one-liner using update and a generator:
dict_b.update((k, v.pop()) for k, v in dict_a.items() if len(v) == 1)
Algorithmically, this doesn't gain anything, but will utilize some optimzations that come with the used syntactic means.
edited Nov 23 '18 at 16:23
answered Nov 23 '18 at 16:15
schwobasegglschwobaseggl
37.5k32443
37.5k32443
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%2f53449871%2fupdate-dict-value-from-one-to-another-based-on-condition-python%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
This will modify the values in
dict_aas well. Is that what you want?– schwobaseggl
Nov 23 '18 at 16:12
yes, i do not really care about dict_a actually
– jacobcan118
Nov 23 '18 at 16:14