How to index a subdictionary and creating a nested for loop











up vote
2
down vote

favorite












I have a list of lists just like below:



>> new_pd[:3]
[['PokedexNumber',
'Name',
'Type',
'Total',
'HP',
'Attack',
'Defense',
'SpecialAttack',
'SpecialDefense',
'Speed'],
[1.0, 'Bulbasaur', 'GrassPoison', 318.0, 45.0, 49.0, 49.0, 65.0, 65.0, 45.0],
[2.0, 'Ivysaur', 'GrassPoison', 405.0, 60.0, 62.0, 63.0, 80.0, 80.0, 60.0]]


What I want to create is below:



{1.0: {'Attack': 49.0,
'Defense': 49.0,
'HP': 45.0,
'Name': 'Bulbasaur',
'PokedexNumber': 1.0,
'SpecialAttack': 65.0,
'SpecialDefense': 65.0,
'Speed': 45.0,
'Total': 318.0,
'Type': 'GrassPoison'},
2.0: {'Attack': 62.0,
'Defense': 63.0,
'HP': 60.0,
'Name': 'Ivysaur',


So from list of lists, I want to have a big nested dictionary.



To create this, I have written the below code in Python:



new_pd_keys = 
for i in range(1, len(new_pd)):
new_pd_keys.append(new_pd[i][0])


Above, I get all the keys from column 1 and save them to a list called new_pd_keys.



Then use the below code to assign each keys' values:



for i in range(1,len(new_pd)):
new_pd_dict = {key:{'Attack':new_pd[i][5], 'Defense':new_pd[i][6], 'HP':new_pd[i][4],
'Name':new_pd[i][1], 'PokedexNumber':new_pd[i][0], 'SpecialAttack':new_pd[i][7],
'SpecialDefense':new_pd[i][8], 'Speed':new_pd[i][9], 'Total':new_pd[i][3],
'Type':new_pd[i][2]} for key in new_pd_keys }


But what I have is the attributes from my last row. You can see the output below, which are all the same.



{1.0: {'Attack': 110.0,
'Defense': 120.0,
'HP': 80.0,
'Name': 'Volcanion',
'PokedexNumber': 721.0,
'SpecialAttack': 130.0,
'SpecialDefense': 90.0,
'Speed': 70.0,
'Total': 600.0,
'Type': 'FireWater'},
2.0: {'Attack': 110.0,
'Defense': 120.0,
'HP': 80.0,
'Name': 'Volcanion',
'PokedexNumber': 721.0,
'SpecialAttack': 130.0,
'SpecialDefense': 90.0,
'Speed': 70.0,
'Total': 600.0,
'Type': 'FireWater'},


Can you suggest a way to perform the indexing in a better way than mine?










share|improve this question






















  • if you are using a dict where the keys are ascending sequentially, you should be using a list. in this case a list of dicts
    – aydow
    Nov 11 at 22:10















up vote
2
down vote

favorite












I have a list of lists just like below:



>> new_pd[:3]
[['PokedexNumber',
'Name',
'Type',
'Total',
'HP',
'Attack',
'Defense',
'SpecialAttack',
'SpecialDefense',
'Speed'],
[1.0, 'Bulbasaur', 'GrassPoison', 318.0, 45.0, 49.0, 49.0, 65.0, 65.0, 45.0],
[2.0, 'Ivysaur', 'GrassPoison', 405.0, 60.0, 62.0, 63.0, 80.0, 80.0, 60.0]]


What I want to create is below:



{1.0: {'Attack': 49.0,
'Defense': 49.0,
'HP': 45.0,
'Name': 'Bulbasaur',
'PokedexNumber': 1.0,
'SpecialAttack': 65.0,
'SpecialDefense': 65.0,
'Speed': 45.0,
'Total': 318.0,
'Type': 'GrassPoison'},
2.0: {'Attack': 62.0,
'Defense': 63.0,
'HP': 60.0,
'Name': 'Ivysaur',


So from list of lists, I want to have a big nested dictionary.



To create this, I have written the below code in Python:



new_pd_keys = 
for i in range(1, len(new_pd)):
new_pd_keys.append(new_pd[i][0])


Above, I get all the keys from column 1 and save them to a list called new_pd_keys.



Then use the below code to assign each keys' values:



for i in range(1,len(new_pd)):
new_pd_dict = {key:{'Attack':new_pd[i][5], 'Defense':new_pd[i][6], 'HP':new_pd[i][4],
'Name':new_pd[i][1], 'PokedexNumber':new_pd[i][0], 'SpecialAttack':new_pd[i][7],
'SpecialDefense':new_pd[i][8], 'Speed':new_pd[i][9], 'Total':new_pd[i][3],
'Type':new_pd[i][2]} for key in new_pd_keys }


But what I have is the attributes from my last row. You can see the output below, which are all the same.



{1.0: {'Attack': 110.0,
'Defense': 120.0,
'HP': 80.0,
'Name': 'Volcanion',
'PokedexNumber': 721.0,
'SpecialAttack': 130.0,
'SpecialDefense': 90.0,
'Speed': 70.0,
'Total': 600.0,
'Type': 'FireWater'},
2.0: {'Attack': 110.0,
'Defense': 120.0,
'HP': 80.0,
'Name': 'Volcanion',
'PokedexNumber': 721.0,
'SpecialAttack': 130.0,
'SpecialDefense': 90.0,
'Speed': 70.0,
'Total': 600.0,
'Type': 'FireWater'},


Can you suggest a way to perform the indexing in a better way than mine?










share|improve this question






















  • if you are using a dict where the keys are ascending sequentially, you should be using a list. in this case a list of dicts
    – aydow
    Nov 11 at 22:10













up vote
2
down vote

favorite









up vote
2
down vote

favorite











I have a list of lists just like below:



>> new_pd[:3]
[['PokedexNumber',
'Name',
'Type',
'Total',
'HP',
'Attack',
'Defense',
'SpecialAttack',
'SpecialDefense',
'Speed'],
[1.0, 'Bulbasaur', 'GrassPoison', 318.0, 45.0, 49.0, 49.0, 65.0, 65.0, 45.0],
[2.0, 'Ivysaur', 'GrassPoison', 405.0, 60.0, 62.0, 63.0, 80.0, 80.0, 60.0]]


What I want to create is below:



{1.0: {'Attack': 49.0,
'Defense': 49.0,
'HP': 45.0,
'Name': 'Bulbasaur',
'PokedexNumber': 1.0,
'SpecialAttack': 65.0,
'SpecialDefense': 65.0,
'Speed': 45.0,
'Total': 318.0,
'Type': 'GrassPoison'},
2.0: {'Attack': 62.0,
'Defense': 63.0,
'HP': 60.0,
'Name': 'Ivysaur',


So from list of lists, I want to have a big nested dictionary.



To create this, I have written the below code in Python:



new_pd_keys = 
for i in range(1, len(new_pd)):
new_pd_keys.append(new_pd[i][0])


Above, I get all the keys from column 1 and save them to a list called new_pd_keys.



Then use the below code to assign each keys' values:



for i in range(1,len(new_pd)):
new_pd_dict = {key:{'Attack':new_pd[i][5], 'Defense':new_pd[i][6], 'HP':new_pd[i][4],
'Name':new_pd[i][1], 'PokedexNumber':new_pd[i][0], 'SpecialAttack':new_pd[i][7],
'SpecialDefense':new_pd[i][8], 'Speed':new_pd[i][9], 'Total':new_pd[i][3],
'Type':new_pd[i][2]} for key in new_pd_keys }


But what I have is the attributes from my last row. You can see the output below, which are all the same.



{1.0: {'Attack': 110.0,
'Defense': 120.0,
'HP': 80.0,
'Name': 'Volcanion',
'PokedexNumber': 721.0,
'SpecialAttack': 130.0,
'SpecialDefense': 90.0,
'Speed': 70.0,
'Total': 600.0,
'Type': 'FireWater'},
2.0: {'Attack': 110.0,
'Defense': 120.0,
'HP': 80.0,
'Name': 'Volcanion',
'PokedexNumber': 721.0,
'SpecialAttack': 130.0,
'SpecialDefense': 90.0,
'Speed': 70.0,
'Total': 600.0,
'Type': 'FireWater'},


Can you suggest a way to perform the indexing in a better way than mine?










share|improve this question













I have a list of lists just like below:



>> new_pd[:3]
[['PokedexNumber',
'Name',
'Type',
'Total',
'HP',
'Attack',
'Defense',
'SpecialAttack',
'SpecialDefense',
'Speed'],
[1.0, 'Bulbasaur', 'GrassPoison', 318.0, 45.0, 49.0, 49.0, 65.0, 65.0, 45.0],
[2.0, 'Ivysaur', 'GrassPoison', 405.0, 60.0, 62.0, 63.0, 80.0, 80.0, 60.0]]


What I want to create is below:



{1.0: {'Attack': 49.0,
'Defense': 49.0,
'HP': 45.0,
'Name': 'Bulbasaur',
'PokedexNumber': 1.0,
'SpecialAttack': 65.0,
'SpecialDefense': 65.0,
'Speed': 45.0,
'Total': 318.0,
'Type': 'GrassPoison'},
2.0: {'Attack': 62.0,
'Defense': 63.0,
'HP': 60.0,
'Name': 'Ivysaur',


So from list of lists, I want to have a big nested dictionary.



To create this, I have written the below code in Python:



new_pd_keys = 
for i in range(1, len(new_pd)):
new_pd_keys.append(new_pd[i][0])


Above, I get all the keys from column 1 and save them to a list called new_pd_keys.



Then use the below code to assign each keys' values:



for i in range(1,len(new_pd)):
new_pd_dict = {key:{'Attack':new_pd[i][5], 'Defense':new_pd[i][6], 'HP':new_pd[i][4],
'Name':new_pd[i][1], 'PokedexNumber':new_pd[i][0], 'SpecialAttack':new_pd[i][7],
'SpecialDefense':new_pd[i][8], 'Speed':new_pd[i][9], 'Total':new_pd[i][3],
'Type':new_pd[i][2]} for key in new_pd_keys }


But what I have is the attributes from my last row. You can see the output below, which are all the same.



{1.0: {'Attack': 110.0,
'Defense': 120.0,
'HP': 80.0,
'Name': 'Volcanion',
'PokedexNumber': 721.0,
'SpecialAttack': 130.0,
'SpecialDefense': 90.0,
'Speed': 70.0,
'Total': 600.0,
'Type': 'FireWater'},
2.0: {'Attack': 110.0,
'Defense': 120.0,
'HP': 80.0,
'Name': 'Volcanion',
'PokedexNumber': 721.0,
'SpecialAttack': 130.0,
'SpecialDefense': 90.0,
'Speed': 70.0,
'Total': 600.0,
'Type': 'FireWater'},


Can you suggest a way to perform the indexing in a better way than mine?







python dictionary for-loop indexing






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 11 at 21:52









ykesen

555




555












  • if you are using a dict where the keys are ascending sequentially, you should be using a list. in this case a list of dicts
    – aydow
    Nov 11 at 22:10


















  • if you are using a dict where the keys are ascending sequentially, you should be using a list. in this case a list of dicts
    – aydow
    Nov 11 at 22:10
















if you are using a dict where the keys are ascending sequentially, you should be using a list. in this case a list of dicts
– aydow
Nov 11 at 22:10




if you are using a dict where the keys are ascending sequentially, you should be using a list. in this case a list of dicts
– aydow
Nov 11 at 22:10












1 Answer
1






active

oldest

votes

















up vote
2
down vote













new_thing = {q[0]: dict(zip(thing[0], q[1:])) for q in thing[1:]}





share|improve this answer























  • Thanks! It works, I will just make small changes to update the column order.
    – ykesen
    Nov 11 at 23:40











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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53253616%2fhow-to-index-a-subdictionary-and-creating-a-nested-for-loop%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








up vote
2
down vote













new_thing = {q[0]: dict(zip(thing[0], q[1:])) for q in thing[1:]}





share|improve this answer























  • Thanks! It works, I will just make small changes to update the column order.
    – ykesen
    Nov 11 at 23:40















up vote
2
down vote













new_thing = {q[0]: dict(zip(thing[0], q[1:])) for q in thing[1:]}





share|improve this answer























  • Thanks! It works, I will just make small changes to update the column order.
    – ykesen
    Nov 11 at 23:40













up vote
2
down vote










up vote
2
down vote









new_thing = {q[0]: dict(zip(thing[0], q[1:])) for q in thing[1:]}





share|improve this answer














new_thing = {q[0]: dict(zip(thing[0], q[1:])) for q in thing[1:]}






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 11 at 22:04

























answered Nov 11 at 21:58









MegaBluejay

33618




33618












  • Thanks! It works, I will just make small changes to update the column order.
    – ykesen
    Nov 11 at 23:40


















  • Thanks! It works, I will just make small changes to update the column order.
    – ykesen
    Nov 11 at 23:40
















Thanks! It works, I will just make small changes to update the column order.
– ykesen
Nov 11 at 23:40




Thanks! It works, I will just make small changes to update the column order.
– ykesen
Nov 11 at 23:40


















draft saved

draft discarded




















































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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53253616%2fhow-to-index-a-subdictionary-and-creating-a-nested-for-loop%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

Florida Star v. B. J. F.

Error while running script in elastic search , gateway timeout

Adding quotations to stringified JSON object values