# Define dict:
dict = {
'k1': 'some data',
7: ['can', 'be', 'a', 'list']
}
# Dictionaries are not position oriented, they are key oriented
# To get the data:
dict['k1'] # prints 'some data'
print(dict[7]) # prints ['can', 'be', 'a', 'list']
Dictionaries are mutable and can be changed
dict[7] = 'new value'
You cannot sort dictionaries, you should use keys to get the data
people_weight_dict = {
'john': 123,
'mike': 170,
'robert': 167
}
# To change john's weight you would do
people_weight_dict['john'] = 134
You can pop items from dictionaries
people_weight_dict.pop('mike')
Syntax:
dict.pop('key')
Clear method
people_weight_dict.clear()
# It will still be a dictionary but it will be an empty one
Adding items to dictionary:
people_weight_dict = {
'john': 123,
'mike': 170,
'robert': 167
}
# Adding the data
people_weight_dict['new_key'] = 'new data to be added'
# At this point the dict has been permanently modified and adjusted with a new entry