Accessing values from dictionaries nested in a list
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
First question here!
countries = [{'country': 'Italy', 'size':3,'reg':9},
{'country': 'Germany', 'size':7,'reg':1},
{'country': 'USA', 'size':9,'reg':4},
]
weights = {'size' : 100, 'reg' : 30}
I am trying to multiply values from the 'countries' nested dictionaries with the value associated with the matching key in the 'weights' dictionary. I tried a for loop approach as the values in 'weights' will be updated by the user.
I have tried this:
countries_weighted = copy.deepcopy(countries)
for key in weights.items():
for i in countries_weighted:
countries_weighted[i][key] *= weights[key]
That doesn't seem to work:
-
TypeError Traceback (most recent call last)
<ipython-input-52-9753dabe7648> in <module>()
13 for key in weights.items():
14 for i in countries_weighted:
---> 15 countries_weighted[i][key] *= weights[key]
16
TypeError: list indices must be integers or slices, not dict
Any idea? Thanks in advance.
python python-3.x list dictionary for-loop
add a comment |
First question here!
countries = [{'country': 'Italy', 'size':3,'reg':9},
{'country': 'Germany', 'size':7,'reg':1},
{'country': 'USA', 'size':9,'reg':4},
]
weights = {'size' : 100, 'reg' : 30}
I am trying to multiply values from the 'countries' nested dictionaries with the value associated with the matching key in the 'weights' dictionary. I tried a for loop approach as the values in 'weights' will be updated by the user.
I have tried this:
countries_weighted = copy.deepcopy(countries)
for key in weights.items():
for i in countries_weighted:
countries_weighted[i][key] *= weights[key]
That doesn't seem to work:
-
TypeError Traceback (most recent call last)
<ipython-input-52-9753dabe7648> in <module>()
13 for key in weights.items():
14 for i in countries_weighted:
---> 15 countries_weighted[i][key] *= weights[key]
16
TypeError: list indices must be integers or slices, not dict
Any idea? Thanks in advance.
python python-3.x list dictionary for-loop
for i in countries_weighted:will return the values incountries_weightedto your variablei, not the index. So in this caseigets an entire dictionary each time. So replacingcountries_weighted[i][key] *= weights[key]withi[key] *= weights[key]should work for you. But Esteban Quiros's code below is much cleaner
– Dan
Nov 22 '18 at 16:47
add a comment |
First question here!
countries = [{'country': 'Italy', 'size':3,'reg':9},
{'country': 'Germany', 'size':7,'reg':1},
{'country': 'USA', 'size':9,'reg':4},
]
weights = {'size' : 100, 'reg' : 30}
I am trying to multiply values from the 'countries' nested dictionaries with the value associated with the matching key in the 'weights' dictionary. I tried a for loop approach as the values in 'weights' will be updated by the user.
I have tried this:
countries_weighted = copy.deepcopy(countries)
for key in weights.items():
for i in countries_weighted:
countries_weighted[i][key] *= weights[key]
That doesn't seem to work:
-
TypeError Traceback (most recent call last)
<ipython-input-52-9753dabe7648> in <module>()
13 for key in weights.items():
14 for i in countries_weighted:
---> 15 countries_weighted[i][key] *= weights[key]
16
TypeError: list indices must be integers or slices, not dict
Any idea? Thanks in advance.
python python-3.x list dictionary for-loop
First question here!
countries = [{'country': 'Italy', 'size':3,'reg':9},
{'country': 'Germany', 'size':7,'reg':1},
{'country': 'USA', 'size':9,'reg':4},
]
weights = {'size' : 100, 'reg' : 30}
I am trying to multiply values from the 'countries' nested dictionaries with the value associated with the matching key in the 'weights' dictionary. I tried a for loop approach as the values in 'weights' will be updated by the user.
I have tried this:
countries_weighted = copy.deepcopy(countries)
for key in weights.items():
for i in countries_weighted:
countries_weighted[i][key] *= weights[key]
That doesn't seem to work:
-
TypeError Traceback (most recent call last)
<ipython-input-52-9753dabe7648> in <module>()
13 for key in weights.items():
14 for i in countries_weighted:
---> 15 countries_weighted[i][key] *= weights[key]
16
TypeError: list indices must be integers or slices, not dict
Any idea? Thanks in advance.
python python-3.x list dictionary for-loop
python python-3.x list dictionary for-loop
edited Nov 22 '18 at 16:50
jpp
103k2167117
103k2167117
asked Nov 22 '18 at 16:36
dpppstldpppstl
1
1
for i in countries_weighted:will return the values incountries_weightedto your variablei, not the index. So in this caseigets an entire dictionary each time. So replacingcountries_weighted[i][key] *= weights[key]withi[key] *= weights[key]should work for you. But Esteban Quiros's code below is much cleaner
– Dan
Nov 22 '18 at 16:47
add a comment |
for i in countries_weighted:will return the values incountries_weightedto your variablei, not the index. So in this caseigets an entire dictionary each time. So replacingcountries_weighted[i][key] *= weights[key]withi[key] *= weights[key]should work for you. But Esteban Quiros's code below is much cleaner
– Dan
Nov 22 '18 at 16:47
for i in countries_weighted: will return the values in countries_weighted to your variable i, not the index. So in this case i gets an entire dictionary each time. So replacing countries_weighted[i][key] *= weights[key] with i[key] *= weights[key] should work for you. But Esteban Quiros's code below is much cleaner– Dan
Nov 22 '18 at 16:47
for i in countries_weighted: will return the values in countries_weighted to your variable i, not the index. So in this case i gets an entire dictionary each time. So replacing countries_weighted[i][key] *= weights[key] with i[key] *= weights[key] should work for you. But Esteban Quiros's code below is much cleaner– Dan
Nov 22 '18 at 16:47
add a comment |
3 Answers
3
active
oldest
votes
You could do it like this:
countries = [{'country': 'Italy', 'size':3,'reg':9},
{'country': 'Germany', 'size':7,'reg':1},
{'country': 'USA', 'size':9,'reg':4},
]
weights = {'size' : 100, 'reg' : 30}
for country in countries:
for key in weights.keys():
country[key] *= weights[key]
print(countries)
Makes sense.hank you Esteban Quiros, Dan and JPP for your help!
– dpppstl
Nov 23 '18 at 15:12
add a comment |
There are a couple of issues:
dict.itemscycles key-value pairs, not just keys;- when you iterate
countries_weightedyou should usei.
So you can amend as follows:
for key, value in weights.items():
for i in countries_weighted:
i[key] *= value
add a comment |
Only need to write countries_weighted[i][key] *= weights[key] as i[key] *= weights[key].
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%2f53435144%2faccessing-values-from-dictionaries-nested-in-a-list%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
You could do it like this:
countries = [{'country': 'Italy', 'size':3,'reg':9},
{'country': 'Germany', 'size':7,'reg':1},
{'country': 'USA', 'size':9,'reg':4},
]
weights = {'size' : 100, 'reg' : 30}
for country in countries:
for key in weights.keys():
country[key] *= weights[key]
print(countries)
Makes sense.hank you Esteban Quiros, Dan and JPP for your help!
– dpppstl
Nov 23 '18 at 15:12
add a comment |
You could do it like this:
countries = [{'country': 'Italy', 'size':3,'reg':9},
{'country': 'Germany', 'size':7,'reg':1},
{'country': 'USA', 'size':9,'reg':4},
]
weights = {'size' : 100, 'reg' : 30}
for country in countries:
for key in weights.keys():
country[key] *= weights[key]
print(countries)
Makes sense.hank you Esteban Quiros, Dan and JPP for your help!
– dpppstl
Nov 23 '18 at 15:12
add a comment |
You could do it like this:
countries = [{'country': 'Italy', 'size':3,'reg':9},
{'country': 'Germany', 'size':7,'reg':1},
{'country': 'USA', 'size':9,'reg':4},
]
weights = {'size' : 100, 'reg' : 30}
for country in countries:
for key in weights.keys():
country[key] *= weights[key]
print(countries)
You could do it like this:
countries = [{'country': 'Italy', 'size':3,'reg':9},
{'country': 'Germany', 'size':7,'reg':1},
{'country': 'USA', 'size':9,'reg':4},
]
weights = {'size' : 100, 'reg' : 30}
for country in countries:
for key in weights.keys():
country[key] *= weights[key]
print(countries)
edited Nov 22 '18 at 16:47
answered Nov 22 '18 at 16:42
Esteban QuirosEsteban Quiros
1015
1015
Makes sense.hank you Esteban Quiros, Dan and JPP for your help!
– dpppstl
Nov 23 '18 at 15:12
add a comment |
Makes sense.hank you Esteban Quiros, Dan and JPP for your help!
– dpppstl
Nov 23 '18 at 15:12
Makes sense.hank you Esteban Quiros, Dan and JPP for your help!
– dpppstl
Nov 23 '18 at 15:12
Makes sense.hank you Esteban Quiros, Dan and JPP for your help!
– dpppstl
Nov 23 '18 at 15:12
add a comment |
There are a couple of issues:
dict.itemscycles key-value pairs, not just keys;- when you iterate
countries_weightedyou should usei.
So you can amend as follows:
for key, value in weights.items():
for i in countries_weighted:
i[key] *= value
add a comment |
There are a couple of issues:
dict.itemscycles key-value pairs, not just keys;- when you iterate
countries_weightedyou should usei.
So you can amend as follows:
for key, value in weights.items():
for i in countries_weighted:
i[key] *= value
add a comment |
There are a couple of issues:
dict.itemscycles key-value pairs, not just keys;- when you iterate
countries_weightedyou should usei.
So you can amend as follows:
for key, value in weights.items():
for i in countries_weighted:
i[key] *= value
There are a couple of issues:
dict.itemscycles key-value pairs, not just keys;- when you iterate
countries_weightedyou should usei.
So you can amend as follows:
for key, value in weights.items():
for i in countries_weighted:
i[key] *= value
answered Nov 22 '18 at 16:45
jppjpp
103k2167117
103k2167117
add a comment |
add a comment |
Only need to write countries_weighted[i][key] *= weights[key] as i[key] *= weights[key].
add a comment |
Only need to write countries_weighted[i][key] *= weights[key] as i[key] *= weights[key].
add a comment |
Only need to write countries_weighted[i][key] *= weights[key] as i[key] *= weights[key].
Only need to write countries_weighted[i][key] *= weights[key] as i[key] *= weights[key].
answered Dec 4 '18 at 9:10
mengxunmengxun
414
414
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%2f53435144%2faccessing-values-from-dictionaries-nested-in-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
for i in countries_weighted:will return the values incountries_weightedto your variablei, not the index. So in this caseigets an entire dictionary each time. So replacingcountries_weighted[i][key] *= weights[key]withi[key] *= weights[key]should work for you. But Esteban Quiros's code below is much cleaner– Dan
Nov 22 '18 at 16:47