Creating the students' records automatically


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

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”] =

<do not remove the three backticks above>


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


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?


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 = []

the other thing you can do is this:

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