car = {
'name': 'Nexon',
'brand': 'TATA',
'type': 'EV',
'vendor': 'Ztx'
}
for key in car:
print(key, end=": ")
print(car[key])
# name Nexon
# brand TATA
# type EV
# vendor Ztx
A dictionary is a key-value pair data assignment in python. If you have used a dictionary in python and you want to use the values of dictionary keys in your code then you can use For loop to access them.
items = {
'key_1': 'value_1',
'key_2': 'value_2',
'key_3': 'value_3'
}
for key in items:
print(key)
# -> key_1
# -> key_2
# -> key_3
items = {
'key_1': 'value_1',
'key_2': 'value_2',
'key_3': 'value_3'
}
for key in items.values():
print(key)
# -> value_1
# -> value_2
# -> value_3
companies = {
'microsoft': 'BillGates',
'apple': 'Steve Jobs',
'resla': 'Elon Musk',
'google': 'Larry Page'
}
for key, value in companies.items():
print(key, ': ', value)
# microsoft : BillGates
# apple : Steve Jobs
# resla : Elon Musk
# google : Larry Page
0 Comments