xxxxxxxxxx
# Loop through dictionary in key order
di = {'b': 2, 'c': 3, 'a': 1}
for k, v in sorted(di.items()):
print(k, v)
# a 1
# b 2
# c 3
xxxxxxxxxx
dictionary = {52:"E",126:"A",134:"B",188:"C",189:"D"}
for key, value in dictionary.items():
print(key)
print(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
d = {'x': 1, 'y': 2, 'z': 3}
for key in d:
print key, 'corresponds to', d[key]
xxxxxxxxxx
python = {
"year released": 2001,
"creater":"Guido Van Rossum"
}
for x in python.values():
print(x)
xxxxxxxxxx
my_dict = {"name": "John", "age": 30, "city": "New York"}
# Iterate over keys
for key in my_dict:
print(key)
xxxxxxxxxx
foreach (var (key, value) in someDictionary) // loop through key and value; WARNING: NON-MUTABLE