Learning Python -Dictionaries
Continuing with my notes on learning Python. A brief overview of Dictionaries in Python from the Python Dev Bootcamp.
- Are unordered mappings
- Key-value pair
- Do not need to know the exact index position
- Just need to know the key in order to know the value
- Uses curly braces and colons to signify the keys and their associated values
- {‘key1′:’value1′,’key2′:’value2’}
- Dictionaries are unordered and cannot be sorted.
- Cannot sort a dictionary
- Lists on the other hand can be retrieved by location
- Dictionaries can hold multiple data types include holding lists, or a dictionary within a dictionary
- Calling a list within a key
- d = {‘k1′:123,’k2′:[0,1,5],’k3’:{‘insideKey’:100}}
- d[‘k2’][2]
- You get 5
- Appending to a dictionary
- d[‘key4’] = 300
- Overwrite
- d[‘k2’]=26
- Additional Functions
- .keys() – shows you a list of keys in the dictionary
- .values() – shows you the list of values in the dictionary
- .items() – shows you the list of pairings
- dict_items([(‘k1’, ‘New Number’), (‘k2’, [0, 1, 5]), (‘k3’, {‘insideKey1’: 100, ‘insidekey2’: 125}), (‘k4’, 75.5)])
- ^ Shown as a Tuple
How I got into Learning to Code in Python.