Watch Me Code 3: List of Dictionary


In [3]:
students = [ 
 { 'Name':'bob','GPA':3.4, 'Ischool' : True },
 { 'Name':'sue','GPA':2.8, 'Ischool' : True },
 { 'Name':'kent','GPA':4.0, 'Ischool' : False }
]
print(students)


[{'Ischool': True, 'Name': 'bob', 'GPA': 3.4}, {'Ischool': True, 'Name': 'sue', 'GPA': 2.8}, {'Ischool': False, 'Name': 'kent', 'GPA': 4.0}]

In [4]:
type(students)


Out[4]:
list

In [9]:
students[-1]


Out[9]:
{'GPA': 4.0, 'Ischool': False, 'Name': 'kent'}

In [5]:
type(students[-1])


Out[5]:
dict

In [6]:
print(students[-1])


{'Ischool': False, 'Name': 'kent', 'GPA': 4.0}

In [7]:
# print names and GPA's of just ischool students:
for student in students:    # list 
    if student['Ischool']:  # == True is not necessary
        print(student['Name'], student['GPA'])


bob 3.4
sue 2.8

In [7]:
students = [
    {"Name": "bob", "age": 18, "grades": [70, 80, 30]},
    {"Name": "Tom", "age": 20, "grades": [70, 80, 30]},
    {"Name": "Jerry", "age": 19, "grades": [70, 80, 30]}
]

for student in students:
    print("Grades for: " + student["Name"])
    for grade in student["grades"]:
        print(grade)


Grades for: bob
70
80
30
Grades for: Tom
70
80
30
Grades for: Jerry
70
80
30

In [ ]: