How can I update list values inside a dictionary in python
I have a dictionary like this:
perfect_data = {
"1": [1, 1, 0, 0, 0, 1, 1, 1,
1, 1, 0, 0, 0, 1, 1, 1,
1, 1, 1, 0, 0, 1, 1, 1,
1, 1, 1, 0, 0, 1, 1, 1,
1, 1, 1, 0, 0, 1, 1, 1,
1, 1, 1, 0, 0, 1, 1, 1,
1, 1, 1, 0, 0, 1, 1, 1,
1, 1, 1, 0, 0, 1, 1, 1 ], }
I need to replace 0 with -1 in this dict.
It's what I tried:
for key in perfect_data.keys():
perfect_data[key]*=2-1
print(perfect_data[key])
But nothing changes when I print each item.
python list dictionary
add a comment |
I have a dictionary like this:
perfect_data = {
"1": [1, 1, 0, 0, 0, 1, 1, 1,
1, 1, 0, 0, 0, 1, 1, 1,
1, 1, 1, 0, 0, 1, 1, 1,
1, 1, 1, 0, 0, 1, 1, 1,
1, 1, 1, 0, 0, 1, 1, 1,
1, 1, 1, 0, 0, 1, 1, 1,
1, 1, 1, 0, 0, 1, 1, 1,
1, 1, 1, 0, 0, 1, 1, 1 ], }
I need to replace 0 with -1 in this dict.
It's what I tried:
for key in perfect_data.keys():
perfect_data[key]*=2-1
print(perfect_data[key])
But nothing changes when I print each item.
python list dictionary
1
x *= 2-1is the same asx *= 1.
– interjay
Dec 27 at 14:28
okey, how can I multiply each item of a list by two and then subtract 1 from it?
– Ahmad
Dec 27 at 14:30
and multiplying a list doesn't yield the result you'd expect anyway <g>
– bruno desthuilliers
Dec 27 at 14:30
add a comment |
I have a dictionary like this:
perfect_data = {
"1": [1, 1, 0, 0, 0, 1, 1, 1,
1, 1, 0, 0, 0, 1, 1, 1,
1, 1, 1, 0, 0, 1, 1, 1,
1, 1, 1, 0, 0, 1, 1, 1,
1, 1, 1, 0, 0, 1, 1, 1,
1, 1, 1, 0, 0, 1, 1, 1,
1, 1, 1, 0, 0, 1, 1, 1,
1, 1, 1, 0, 0, 1, 1, 1 ], }
I need to replace 0 with -1 in this dict.
It's what I tried:
for key in perfect_data.keys():
perfect_data[key]*=2-1
print(perfect_data[key])
But nothing changes when I print each item.
python list dictionary
I have a dictionary like this:
perfect_data = {
"1": [1, 1, 0, 0, 0, 1, 1, 1,
1, 1, 0, 0, 0, 1, 1, 1,
1, 1, 1, 0, 0, 1, 1, 1,
1, 1, 1, 0, 0, 1, 1, 1,
1, 1, 1, 0, 0, 1, 1, 1,
1, 1, 1, 0, 0, 1, 1, 1,
1, 1, 1, 0, 0, 1, 1, 1,
1, 1, 1, 0, 0, 1, 1, 1 ], }
I need to replace 0 with -1 in this dict.
It's what I tried:
for key in perfect_data.keys():
perfect_data[key]*=2-1
print(perfect_data[key])
But nothing changes when I print each item.
python list dictionary
python list dictionary
edited Dec 27 at 14:31
asked Dec 27 at 14:25
Ahmad
2,76333058
2,76333058
1
x *= 2-1is the same asx *= 1.
– interjay
Dec 27 at 14:28
okey, how can I multiply each item of a list by two and then subtract 1 from it?
– Ahmad
Dec 27 at 14:30
and multiplying a list doesn't yield the result you'd expect anyway <g>
– bruno desthuilliers
Dec 27 at 14:30
add a comment |
1
x *= 2-1is the same asx *= 1.
– interjay
Dec 27 at 14:28
okey, how can I multiply each item of a list by two and then subtract 1 from it?
– Ahmad
Dec 27 at 14:30
and multiplying a list doesn't yield the result you'd expect anyway <g>
– bruno desthuilliers
Dec 27 at 14:30
1
1
x *= 2-1 is the same as x *= 1.– interjay
Dec 27 at 14:28
x *= 2-1 is the same as x *= 1.– interjay
Dec 27 at 14:28
okey, how can I multiply each item of a list by two and then subtract 1 from it?
– Ahmad
Dec 27 at 14:30
okey, how can I multiply each item of a list by two and then subtract 1 from it?
– Ahmad
Dec 27 at 14:30
and multiplying a list doesn't yield the result you'd expect anyway <g>
– bruno desthuilliers
Dec 27 at 14:30
and multiplying a list doesn't yield the result you'd expect anyway <g>
– bruno desthuilliers
Dec 27 at 14:30
add a comment |
5 Answers
5
active
oldest
votes
Replace this line perfect_data[key]*=2-1 with perfect_data[key] = [x*2 -1 for x in perfect_data[key]]
for key in perfect_data.keys():
perfect_data[key] = [x*2 -1 for x in perfect_data[key]]
print(perfect_data[key])
Output:
{'1': [1, 1, -1, -1, -1, 1, 1, 1, 1, 1, -1, -1, -1, 1, 1, 1, 1, 1, 1,
-1, -1, 1, 1, 1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1, -1, 1, 1, 1,
1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1, -1, 1, 1, 1]}
add a comment |
It's what I tried and worked:
for key,value in perfect_data.items():
perfect_data[key]=[2*x-1 for x in value]
print(perfect_data[key])
add a comment |
No idea, what are you trying to achieve, but if strictly following your question, the answer is:
perfect_data = {
"1": [1, 1, 0, 0, 0, 1, 1, 1,
1, 1, 0, 0, 0, 1, 1, 1,
1, 1, 1, 0, 0, 1, 1, 1,
1, 1, 1, 0, 0, 1, 1, 1,
1, 1, 1, 0, 0, 1, 1, 1,
1, 1, 1, 0, 0, 1, 1, 1,
1, 1, 1, 0, 0, 1, 1, 1,
1, 1, 1, 0, 0, 1, 1, 1 ],
}
for k,v in perfect_data.items():
perfect_data[k] = [x or -1 for x in v]
print(perfect_data)
Output
{'1': [1, 1, -1, -1, -1, 1, 1, 1, 1, 1, -1, -1, -1, 1, 1, 1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1, -1, 1, 1, 1]}
Statement x or -1 uses the fact that 0 gives False and or-operator returns first non-false argument.
add a comment |
If I get the point, try this way:
for value in perfect_data.values():
for i, e in enumerate(value):
if e == 0:
value[i] = -1
print(perfect_data)
if you only want the dict's values, usedict.values();-)
– bruno desthuilliers
Dec 27 at 14:34
@brunodesthuilliers, why not? Thanks!
– iGian
Dec 27 at 14:35
add a comment |
As simple as this:
perfect_data['1'] = [i if i else -1 for i in perfect_data['1']]
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%2f53946588%2fhow-can-i-update-list-values-inside-a-dictionary-in-python%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
Replace this line perfect_data[key]*=2-1 with perfect_data[key] = [x*2 -1 for x in perfect_data[key]]
for key in perfect_data.keys():
perfect_data[key] = [x*2 -1 for x in perfect_data[key]]
print(perfect_data[key])
Output:
{'1': [1, 1, -1, -1, -1, 1, 1, 1, 1, 1, -1, -1, -1, 1, 1, 1, 1, 1, 1,
-1, -1, 1, 1, 1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1, -1, 1, 1, 1,
1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1, -1, 1, 1, 1]}
add a comment |
Replace this line perfect_data[key]*=2-1 with perfect_data[key] = [x*2 -1 for x in perfect_data[key]]
for key in perfect_data.keys():
perfect_data[key] = [x*2 -1 for x in perfect_data[key]]
print(perfect_data[key])
Output:
{'1': [1, 1, -1, -1, -1, 1, 1, 1, 1, 1, -1, -1, -1, 1, 1, 1, 1, 1, 1,
-1, -1, 1, 1, 1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1, -1, 1, 1, 1,
1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1, -1, 1, 1, 1]}
add a comment |
Replace this line perfect_data[key]*=2-1 with perfect_data[key] = [x*2 -1 for x in perfect_data[key]]
for key in perfect_data.keys():
perfect_data[key] = [x*2 -1 for x in perfect_data[key]]
print(perfect_data[key])
Output:
{'1': [1, 1, -1, -1, -1, 1, 1, 1, 1, 1, -1, -1, -1, 1, 1, 1, 1, 1, 1,
-1, -1, 1, 1, 1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1, -1, 1, 1, 1,
1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1, -1, 1, 1, 1]}
Replace this line perfect_data[key]*=2-1 with perfect_data[key] = [x*2 -1 for x in perfect_data[key]]
for key in perfect_data.keys():
perfect_data[key] = [x*2 -1 for x in perfect_data[key]]
print(perfect_data[key])
Output:
{'1': [1, 1, -1, -1, -1, 1, 1, 1, 1, 1, -1, -1, -1, 1, 1, 1, 1, 1, 1,
-1, -1, 1, 1, 1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1, -1, 1, 1, 1,
1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1, -1, 1, 1, 1]}
answered Dec 27 at 14:35
Sam
556416
556416
add a comment |
add a comment |
It's what I tried and worked:
for key,value in perfect_data.items():
perfect_data[key]=[2*x-1 for x in value]
print(perfect_data[key])
add a comment |
It's what I tried and worked:
for key,value in perfect_data.items():
perfect_data[key]=[2*x-1 for x in value]
print(perfect_data[key])
add a comment |
It's what I tried and worked:
for key,value in perfect_data.items():
perfect_data[key]=[2*x-1 for x in value]
print(perfect_data[key])
It's what I tried and worked:
for key,value in perfect_data.items():
perfect_data[key]=[2*x-1 for x in value]
print(perfect_data[key])
answered Dec 27 at 14:33
Ahmad
2,76333058
2,76333058
add a comment |
add a comment |
No idea, what are you trying to achieve, but if strictly following your question, the answer is:
perfect_data = {
"1": [1, 1, 0, 0, 0, 1, 1, 1,
1, 1, 0, 0, 0, 1, 1, 1,
1, 1, 1, 0, 0, 1, 1, 1,
1, 1, 1, 0, 0, 1, 1, 1,
1, 1, 1, 0, 0, 1, 1, 1,
1, 1, 1, 0, 0, 1, 1, 1,
1, 1, 1, 0, 0, 1, 1, 1,
1, 1, 1, 0, 0, 1, 1, 1 ],
}
for k,v in perfect_data.items():
perfect_data[k] = [x or -1 for x in v]
print(perfect_data)
Output
{'1': [1, 1, -1, -1, -1, 1, 1, 1, 1, 1, -1, -1, -1, 1, 1, 1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1, -1, 1, 1, 1]}
Statement x or -1 uses the fact that 0 gives False and or-operator returns first non-false argument.
add a comment |
No idea, what are you trying to achieve, but if strictly following your question, the answer is:
perfect_data = {
"1": [1, 1, 0, 0, 0, 1, 1, 1,
1, 1, 0, 0, 0, 1, 1, 1,
1, 1, 1, 0, 0, 1, 1, 1,
1, 1, 1, 0, 0, 1, 1, 1,
1, 1, 1, 0, 0, 1, 1, 1,
1, 1, 1, 0, 0, 1, 1, 1,
1, 1, 1, 0, 0, 1, 1, 1,
1, 1, 1, 0, 0, 1, 1, 1 ],
}
for k,v in perfect_data.items():
perfect_data[k] = [x or -1 for x in v]
print(perfect_data)
Output
{'1': [1, 1, -1, -1, -1, 1, 1, 1, 1, 1, -1, -1, -1, 1, 1, 1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1, -1, 1, 1, 1]}
Statement x or -1 uses the fact that 0 gives False and or-operator returns first non-false argument.
add a comment |
No idea, what are you trying to achieve, but if strictly following your question, the answer is:
perfect_data = {
"1": [1, 1, 0, 0, 0, 1, 1, 1,
1, 1, 0, 0, 0, 1, 1, 1,
1, 1, 1, 0, 0, 1, 1, 1,
1, 1, 1, 0, 0, 1, 1, 1,
1, 1, 1, 0, 0, 1, 1, 1,
1, 1, 1, 0, 0, 1, 1, 1,
1, 1, 1, 0, 0, 1, 1, 1,
1, 1, 1, 0, 0, 1, 1, 1 ],
}
for k,v in perfect_data.items():
perfect_data[k] = [x or -1 for x in v]
print(perfect_data)
Output
{'1': [1, 1, -1, -1, -1, 1, 1, 1, 1, 1, -1, -1, -1, 1, 1, 1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1, -1, 1, 1, 1]}
Statement x or -1 uses the fact that 0 gives False and or-operator returns first non-false argument.
No idea, what are you trying to achieve, but if strictly following your question, the answer is:
perfect_data = {
"1": [1, 1, 0, 0, 0, 1, 1, 1,
1, 1, 0, 0, 0, 1, 1, 1,
1, 1, 1, 0, 0, 1, 1, 1,
1, 1, 1, 0, 0, 1, 1, 1,
1, 1, 1, 0, 0, 1, 1, 1,
1, 1, 1, 0, 0, 1, 1, 1,
1, 1, 1, 0, 0, 1, 1, 1,
1, 1, 1, 0, 0, 1, 1, 1 ],
}
for k,v in perfect_data.items():
perfect_data[k] = [x or -1 for x in v]
print(perfect_data)
Output
{'1': [1, 1, -1, -1, -1, 1, 1, 1, 1, 1, -1, -1, -1, 1, 1, 1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, -1, -1, 1, 1, 1]}
Statement x or -1 uses the fact that 0 gives False and or-operator returns first non-false argument.
answered Dec 27 at 14:33
grapes
2,5471115
2,5471115
add a comment |
add a comment |
If I get the point, try this way:
for value in perfect_data.values():
for i, e in enumerate(value):
if e == 0:
value[i] = -1
print(perfect_data)
if you only want the dict's values, usedict.values();-)
– bruno desthuilliers
Dec 27 at 14:34
@brunodesthuilliers, why not? Thanks!
– iGian
Dec 27 at 14:35
add a comment |
If I get the point, try this way:
for value in perfect_data.values():
for i, e in enumerate(value):
if e == 0:
value[i] = -1
print(perfect_data)
if you only want the dict's values, usedict.values();-)
– bruno desthuilliers
Dec 27 at 14:34
@brunodesthuilliers, why not? Thanks!
– iGian
Dec 27 at 14:35
add a comment |
If I get the point, try this way:
for value in perfect_data.values():
for i, e in enumerate(value):
if e == 0:
value[i] = -1
print(perfect_data)
If I get the point, try this way:
for value in perfect_data.values():
for i, e in enumerate(value):
if e == 0:
value[i] = -1
print(perfect_data)
edited Dec 27 at 14:36
bruno desthuilliers
47.9k54162
47.9k54162
answered Dec 27 at 14:33
iGian
3,2402622
3,2402622
if you only want the dict's values, usedict.values();-)
– bruno desthuilliers
Dec 27 at 14:34
@brunodesthuilliers, why not? Thanks!
– iGian
Dec 27 at 14:35
add a comment |
if you only want the dict's values, usedict.values();-)
– bruno desthuilliers
Dec 27 at 14:34
@brunodesthuilliers, why not? Thanks!
– iGian
Dec 27 at 14:35
if you only want the dict's values, use
dict.values() ;-)– bruno desthuilliers
Dec 27 at 14:34
if you only want the dict's values, use
dict.values() ;-)– bruno desthuilliers
Dec 27 at 14:34
@brunodesthuilliers, why not? Thanks!
– iGian
Dec 27 at 14:35
@brunodesthuilliers, why not? Thanks!
– iGian
Dec 27 at 14:35
add a comment |
As simple as this:
perfect_data['1'] = [i if i else -1 for i in perfect_data['1']]
add a comment |
As simple as this:
perfect_data['1'] = [i if i else -1 for i in perfect_data['1']]
add a comment |
As simple as this:
perfect_data['1'] = [i if i else -1 for i in perfect_data['1']]
As simple as this:
perfect_data['1'] = [i if i else -1 for i in perfect_data['1']]
answered Dec 27 at 14:37
Ssein
1,0161921
1,0161921
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.
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%2f53946588%2fhow-can-i-update-list-values-inside-a-dictionary-in-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
1
x *= 2-1is the same asx *= 1.– interjay
Dec 27 at 14:28
okey, how can I multiply each item of a list by two and then subtract 1 from it?
– Ahmad
Dec 27 at 14:30
and multiplying a list doesn't yield the result you'd expect anyway <g>
– bruno desthuilliers
Dec 27 at 14:30