A Dictionary is an unordered set of key:value pairs, with the requirement that the keys are unique (within a Dictionary). A few pointers about dictionary:
- An empty dictionary can be created by a pair of braces: {}.
- Dictionary elements can be accessed by dictionary keys
- DICT.keys() will return all the keys of given dictionary “DICT”
DICT = {
'Name':'Kunal',
'Company':'Analytics Vidhya'
}
#Dictionary elements can be accessed by keys
print (DICT['Name'])
#The above print statement will print Kunal
In dictionary “DICT”, Name and Company are dictionary keys whereas “Kunal” and “Analytics Vidhya” are their respective values.
Other Tasks:
# Create a dictionary dict1
dict1 = { ‘Age’: 16, ‘Name’: ‘Max’, ‘Sports’: ‘Cricket’}
# Update the value of Age to 18
dict1[‘Age’] = 18
# Print the value of Age
print(dict1[‘Age’])
# Store the keys of dictionary dict1 to dict_keys
dict_keys = dict1.keys()
Try it yourself in DataCamp here