Dictionaries
Dictionaries in Python
# 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'] = 134You can pop items from dictionaries
Syntax:
Clear method
Adding items to dictionary:
Accessing item from a list in the dict
Accessing items from a dictionary in a list in a dict
Remove items for dictionary:
Last updated