xxxxxxxxxx
a_dict = {'color': 'blue', 'fruit': 'apple', 'pet': 'dog'}
for key, value in a_dict.items():
print(key, '->', value)
xxxxxxxxxx
a_dict = {'apple':'red', 'grass':'green', 'sky':'blue'}
for key in a_dict:
print key # for the keys
print a_dict[key] # for the values
xxxxxxxxxx
a_dict = {"color": "blue", "fruit": "apple", "pet": "dog"}
# Will loop through the dict's elements (key, value) WITHOUT ORDER
for key, value in a_dict.items():
print(key, '->', value)
xxxxxxxxxx
students = {
'John': 85,
'Emily': 92,
'Michael': 78,
'Sarah': 89
}
for student in students:
print(student, students[student])
xxxxxxxxxx
my_dict = {'a': 1, 'b': 2, 'c': 3}
for key, value in my_dict.items():
print(key, value)
xxxxxxxxxx
my_dict = {"name": "John", "age": 30, "city": "New York"}
# Iterate over keys
for key in my_dict:
print(key)
xxxxxxxxxx
dictionary_name={"Name": "John", "Age": 29, "salary":25000,"Company":"GOOGLE"}
for key in dictionary_name.keys():
print(key)
xxxxxxxxxx
thisdict = {
"1": "C language",
"2": "Java Language",
"4": "Python Language",
"3": "C++",
}
print('\n\n')
#print 2nd record from dictionary
n=2
for index, (key, value) in enumerate(thisdict.items()):
if index == n:
print(key, '::', value)
break