Looping Through Keys in a Dictionary #
Let's take a look at an example of looping through dictionary data:
my_student = {"name": "James", "age": 10, "gender": "M"}
for i in my_student:
print(i)
Output
# name
# age
# gender
We can see that the data printed out are the keys of the dictionary. We can use this to access the values of the dictionary:
my_student = {"name": "James", "age": 10, "gender": "M"}
for key in my_student:
print(my_student[key])
Output
# James
# 10
# M
Exercise #
Loop through the given dictionary person and append its values to the list properties.
Tests #
- The list
propertiesshould contain"George",35, and"M"
Getting Started with Python
Data Types
Python Functions
Statements in Python
Basic Debugging in Python
Basic Algorithm
Object-Oriented Programming
Error Handling
Intermediate Algorithm
Python Modules