xxxxxxxxxx
dataList = [{'a': 1}, {'b': 3}, {'c': 5}]
for dic in dataList:
for key in dic:
print(dic[key])
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
fav_numbers = {'eric': 17, 'ever': 4}
for number in fav_numbers.values():
print(str(number) + ' is a favorite')
xxxxxxxxxx
d = {'x': 1, 'y': 2, 'z': 3}
for key in d:
print key, 'corresponds to', d[key]
xxxxxxxxxx
students = {
'John': 85,
'Emily': 92,
'Michael': 78,
'Sarah': 89
}
for student in students:
print(student, students[student])
xxxxxxxxxx
list1 = [1,2,3,4]
list2 = ['one','two','three','four']
#Single List convert to Dict
my_Dict = dict()
for index, value in enumerate(list1):
my_Dict[index] = value
print(my_Dict)
# Two list convert to dict
new_dict = dict(zip(list1,list2))
print(new_dict)
xxxxxxxxxx
Titanic_cast = {
"Leonardo DiCaprio": "Jack Dawson",
"Kate Winslet": "Rose Dewitt Bukater",
"Billy Zane": "Cal Hockley",
}
print("Iterating through keys:")
for key in Titanic_cast:
print(key)
print("\nIterating through keys and values:")
for key, value in Titanic_cast.items():
print("Actor/ Actress: {} Role: {}".format(key, value))
# output -
# Iterating through keys:
# Billy Zane
# Leonardo DiCaprio
# Kate Winslet
# Iterating through keys and values:
# Actor/ Actress: Billy Zane Role: Cal Hockley
# Actor/ Actress: Leonardo DiCaprio Role: Jack Dawson
# Actor/ Actress: Kate Winslet Role: Rose Dewitt Bukater
xxxxxxxxxx
python = {
"year released": 2001,
"creater":"Guido Van Rossum"
}
for x in python.values():
print(x)