python 2.7 - Ho can I store data in dictionary with specific keys -


below sample code snippet have been trying.

for example

my_dict = {} my_dict = [] new_dict = {}  student_id = input("enter id: ") name = raw_input("enter name: ") course = raw_input("enter course: ") school = raw_input("enter school: ")  new_dict['student_id'] = student_id new_dict['name'] = name new_dict['course'] = course new_dict['school'] = school  my_dict[student_id].append( new_dict ) 

this codes generates error.

how come expected output? key student_id.

output = [{     '1': {         'student_id': '1',         'name': 'test name 1',         'course': 'course 1',         'school':'school 1'     },     '2': {         'student_id': '2',         'name': 'test name 2',         'course': 'course 2',         'school': 'schoole 2'     }, }] 

i believe mixing list , dictionary functionality.

from desired output looks want dictionary not list , last line in example needs change such:

my_dict = {}  new_dict = {}  student_id = input("enter id: ")  name = raw_input("enter name: ") course = raw_input("enter course: ")  school = raw_input("enter school: ")  new_dict['student_id'] = student_id  new_dict['name'] = name new_dict['course'] = course  new_dict['school'] = school  my_dict[student_id] = new_dict 

Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -