lloyd = {
"name": "Lloyd",
"homework": [90.0, 97.0, 75.0, 92.0],
"quizzes": [88.0, 40.0, 94.0],
"tests": [75.0, 90.0]
}
students = ["lloyd","alice","tyler"]
#Accessing Dictionary from List
print students[0]['name']
Expected out is : Lloyd
whenever I run the above code I get this error,
===============================================
Traceback (most recent call last):
File "python", line 30, in <module>
TypeError: string indices must be integers
===============================================
Any help will be highly appreciated.
it's because students contains just strings ["lloyd","alice","tyler"]
print students[0]
>> "lloyd"
maybe you wanted to do this:
students = [lloyd]
then
print students[0]['name']
should work
You may want to place the dictionaries in the list rather than just the string names.
lloyd = {
"name": "Lloyd",
"homework": [90.0, 97.0, 75.0, 92.0],
"quizzes": [88.0, 40.0, 94.0],
"tests": [75.0, 90.0]
}
alice = {"name": "Alice "}
tyler = {"name": "Tyler "}
students = [lloyd, alice, tyler]
print students[0]['name']
Run the following command:
print students[0]
It will give your desired output.
Currently, students
is just a list of strings. So the value of students[0]
is a string, 'lloyd'
, which is distinct from the dictionary you created with the name lloyd
.
I think you want students
to be a list of dictionaries. Below I've shown how to do that, and created empty dictionaries for alice
and tyler
. Then simply take the quotes away from your declaration of students
to store the dictionaries instead of strings with the same names, and your last line works.
lloyd = {
"name": "Lloyd",
"homework": [90.0, 97.0, 75.0, 92.0],
"quizzes": [88.0, 40.0, 94.0],
"tests": [75.0, 90.0]
}
alice = {"name": "Alice"}
tyler = {"name": "Tyler"}
students = [lloyd,alice,tyler]
print students[0]['name']
which gives:
Lloyd
You are not using the dictionary lloyd defined at the top. When you create your list with students = ["lloyd","alice","tyler"], you are instead using the string "lloyd". So students[0] returns this string instead of the dict.
You need to use students = [lloyd,alice,tyler]. You might also want to create a dict for alice and tyler.
What you have tried is almost correct. You got an error because "string indices must be integers". So you can use eval() method in python ( python-eval-method ).
try the following code
lloyd = {
"name": "Lloyd",
"homework": [90.0, 97.0, 75.0, 92.0],
"quizzes": [88.0, 40.0, 94.0],
"tests": [75.0, 90.0]
}
students = ["lloyd","alice","tyler"]
#Accessing Dictionary from List
print eval(students[0])['name']
It will work
your students
list contains just str
objects.
print students[0]
>> "lloyd"
as you can see we got str
object type.
you should fix it with this usage and removal of the the quotes:
students = [lloyd]
print students[0]['name']
Done ! :)
Even better to use the get
method for dict usage:
print students[0].get('name')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.