1
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.

idjaw
  • 25,487
  • 7
  • 64
  • 83
  • 1
    Possible duplicate http://stackoverflow.com/questions/6077675/why-am-i-seeing-typeerror-string-indices-must-be-integers – CodeChanger Mar 27 '17 at 12:56
  • students = [lloyd] not students = ["lloyd"] – Karthikeyan KR Mar 27 '17 at 12:57
  • Read the error message: The error message means that string indices must be integers. The only index that isn't is `'name'`. So it probably means that `students[0]` is a string (print it to see what it is). Other than that it's unclear what you're trying to achieve. – skyking Mar 27 '17 at 12:58
  • Probably not what you wanted (or what you should want), but you could do `print locals()[students[0]]['name']`. – Paul Rooney Mar 27 '17 at 13:00

7 Answers7

3

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

Václav Struhár
  • 1,739
  • 13
  • 21
1

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']
0

Run the following command:

print students[0]

It will give your desired output.

Dan D.
  • 73,243
  • 15
  • 104
  • 123
0

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
Kewl
  • 3,327
  • 5
  • 26
  • 45
0

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.

0

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

Sarath K S
  • 114
  • 1
  • 7
0

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')
nivhanin
  • 1,688
  • 3
  • 19
  • 31