Defining Multiple Dictionaries Within a Loop in Python -


what i'm looking define 3 similar dictionaries subtle differences. if recognize 1 of problems codeacademy course on python, , i'm looking little more elegantly. anyways, here's have:

import string name in ["lloyd", "alice", "tyler"]:     name = {"name": string.capitalize(name), "homework": [], "quizzes": [], "tests": []} 

this isn't working. want 3 dictionaries, have names "lloyd" "alice" , "tyler" , keys of names (but capitalized), "homework", "quizzes", , "tests"

to clarify, output want equivalent this:

lloyd = {"name": "lloyd", "homework": [], "quizzes": [], "tests": []} alice = {"name": "alice", "homework": [], "quizzes": [], "tests": []} tyler = {"name": "tyler", "homework": [], "quizzes": [], "tests": []} 

i think instead of creating variable each name can create dict of names each name pointing dictionary.

>>> names=["lloyd", "alice", "tyler"] >>> keys=["homework", "quizzes", "tests"] >>> dic={ name.capitalize():{ key:[] key in keys} name in names} >>> dic {'tyler': {'quizzes': [], 'tests': [], 'homework': []},   'lloyd': {'quizzes': [], 'tests': [], 'homework': []},  'alice': {'quizzes': [], 'tests': [], 'homework': []}} 

now access tyler use:

>>> dic['tyler'] {'quizzes': [], 'tests': [], 'homework': []} 

Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -