Dictionaries in Python are created using curly braces {} and consist of key-value pairs separated by colons :. They are unordered collections of key-value pairs that allow for efficient lookup, insertion, and deletion of items based on their keys. Each key in a dictionary must be unique and immutable, such as strings, numbers, or tuples. Keys are used to access corresponding values, which can be any data type, including lists, tuples, or even other dictionaries.
Dataset:
basketball_champions_top10={'Los Angeles Lakers':17,'Boston Celtics':17,'Golden State Warriors':7,'Chicago Bulls':6,'San Antonio Spurs':5,'Philadelphia 76ers':3,'Detroit Pistons':3,'Miami Heat':3,'New York Knicks':2,'Houston Rockets':2,'Milwaukee Bucks':2,}
1. Accessing Dictionaries Elements
# Print out the keys in basketball_championsprint(f"Keys: \n{basketball_champions_top10.keys()}\n")# Print out the values in basketball_championsprint(f"Values: \n{basketball_champions_top10.values()}\n")# Print out all key-value pairs with items()print(f"Items: \n{basketball_champions_top10.items()}\n")# Print out championship counts for 'Boston Celtics'print(f"Boston Celtics championships: {basketball_champions_top10['Boston Celtics']}")#OUTPUT"""Keys: dict_keys(['Los Angeles Lakers', 'Boston Celtics', 'Golden State Warriors', 'Chicago Bulls', 'San Antonio Spurs', 'Philadelphia 76ers', 'Detroit Pistons', 'Miami Heat', 'New York Knicks', 'Houston Rockets'])Values: dict_values([17, 17, 7, 6, 5, 3, 3, 3, 2, 2])Items: dict_items([('Los Angeles Lakers', 17), ('Boston Celtics', 17), ('Golden State Warriors', 7), ('Chicago Bulls', 6), ('San Antonio Spurs', 5), ('Philadelphia 76ers', 3), ('Detroit Pistons', 3), ('Miami Heat', 3), ('New York Knicks', 2), ('Houston Rockets', 2)])Boston Celtics championships: 17"""
Unlike Python Lists , which are indexed by integers, Dictionaries are indexed by unique keys.
2. Updating Dictionaries
2.1. Adding new key-value pairs
# unlike lists, dictionaries dict['new_key']=new_value syntax# add 'Milwaukee Bucks' who has 2 championshipsbasketball_champions_top10 +={'Milwaukee Bucks':2}# Error#OUTPUT"""---------------------------------------------------------------------------TypeError Traceback (most recent call last)Cell In[14], line 4 1 # Adding new key-value pairs 2 # unlike lists, dictionaries dict['new_key']=new_value syntax 3 # add 'Milwaukee Bucks' who has 2 championships----> 4 basketball_champions_top10 += {'Milwaukee Bucks': 2} # ErrorTypeError: unsupported operand type(s) for +=: 'dict' and 'dict'"""# correct waybasketball_champions_top10['Milwaukee Bucks']=2print(basketball_champions_top10)print("---------------------")# or using the method update()basketball_champions_top10.update({'Cleveland Cavaliers': 1})print(basketball_champions_top10)#OUTPUT"""{'Los Angeles Lakers': 17, 'Boston Celtics': 17, 'Golden State Warriors': 7, 'Chicago Bulls': 6, 'San Antonio Spurs': 5, 'Philadelphia 76ers': 3, 'Detroit Pistons': 3, 'Miami Heat': 3, 'New York Knicks': 2, 'Houston Rockets': 2, 'Milwaukee Bucks': 2}---------------------{'Los Angeles Lakers': 17, 'Boston Celtics': 17, 'Golden State Warriors': 7, 'Chicago Bulls': 6, 'San Antonio Spurs': 5, 'Philadelphia 76ers': 3, 'Detroit Pistons': 3, 'Miami Heat': 3, 'New York Knicks': 2, 'Houston Rockets': 2, 'Milwaukee Bucks': 2, 'Cleveland Cavaliers': 1}"""
2.2. Deleting new key-value pairs
# delete 'Cleveland Cavaliers' and 'Milwaukee Bucks' and their values# a. using deldel basketball_champions_top10['Cleveland Cavaliers']# b. using .pop()basketball_champions_top10.pop('Milwaukee Bucks')print(basketball_champions_top10)#OUTPUT"""{'Los Angeles Lakers': 17, 'Boston Celtics': 17, 'Golden State Warriors': 7, 'Chicago Bulls': 6, 'San Antonio Spurs': 5, 'Philadelphia 76ers': 3, 'Detroit Pistons': 3, 'Miami Heat': 3, 'New York Knicks': 2, 'Houston Rockets': 2}"""
Returns a list of all the values in the dictionary
Click on the methods practice them on W3schools
# get the value for a specific keyprint(basketball_champions_top10.get('Boston Celtics'))# remove the last itemprint(basketball_champions_top10.popitem())#OUTPUT"""17('Houston Rockets', 2)"""
Conclusion
Dictionaries in Python are versatile data structures that store key-value pairs. Keys, which must be immutable and unique within a dictionary, are used to access associated values, which can be of any data type. Dictionaries provide a flexible way to organize and manipulate data, supporting operations like insertion, deletion, and modification of key-value pairs. They are commonly used to represent mappings/configurations and are fundamental in Python programming for their ability to provide efficient data organization and retrieval for various applications.