Creating the students' records automatically


#1

I wanted to avoid typing similar structures for all students separately so I tried to use a for loop instead in

https://www.codecademy.com/courses/python-beginner-en-qzsCL/0/1?curriculum_id=4f89dab3d788890003000096

The code I wanted to use was the one below but I received an error

Traceback (most recent call last):
File "python", line 7, in
TypeError: 'str' object does not support item assignment

Is there a way to have python make all these assignments for me? I want Python to simply fill in the name of the student and execute the line of code accordingly


students = ['lloyd','alice','tyer']
for student in students:
    student["name"] = student[0].upper() + student[1:]
    student["homework"] = []
    student["quizzez"] = []
    student["tests"] = []


#2

pythons lists don't support keys + values, use a dictionary instead.


#3

Hi, I didn't add the dicitonaries when posting my question:
the more complete code at the time was

lloyd = {}
alice = {}
tyler = {}
students = ['lloyd','alice','tyler']
for student in students:
    student["name"] = student[0].upper() + student[1:]
    student["homework"] = []
    student["quizzes"] = []
    student["tests"] = []

Is there a way to have Python fill in the name in the code and run that piece of code for all names in the list?


#4

first off, you are doing something which is not possible. Take a look at this code:

students = ['lloyd','alice','tyler']
for student in students:
    print type(student)

student simple refers to the string inside the list, not the dictionary's you defined. what you can do instead, is store a copy of a dictionary in a list:

lloyd = {}
alice = {}
tyler = {}
students = []
students.append(dict(lloyd))

the other thing you can do is this:

students = [{"name": "lloyd"}]