Dictionaries in Python: Unraveling Key-Value Pairs
Dictionaries in Python are powerful data structures that store key-value pairs, allowing efficient retrieval and manipulation of data. Understanding dictionaries is crucial for tasks like representing real-world entities, configuration settings, and more. Let's delve into the characteristics and functionalities of dictionaries:
1. Dictionary Declaration :
- Dictionaries are declared using curly braces {} and consist of key-value pairs separated by colons.
# Dictionary Declaration Example
person = {'name': 'Sakib', 'age': 32, 'city': 'Dhaka'} # Dictionary with key-value pairs
# Output: print(person) # {'name': 'Sakib', 'age': 32, 'city': 'Dhaka'}
# Accessing values using keys print(person['name']) # Sakib
2. Key-Value Pairs :
- Each element in a dictionary is a key-value pair, where keys are unique and used for indexing.
# Key-Value Pairs in a Dictionary Example
person = {'name': 'Rakib', 'age': 28, 'city': 'Rajshahi'} # Dictionary with key-value pairs
# Output:
print(person) # {'name': 'Rakib', 'age': 28, 'city': 'Rajshahi'}
# Accessing values using keys print(person['name']) # Rakib print(person['age']) # 28
- Python provides a variety of built-in methods for dictionary manipulation, including adding items, removing keys, and updating values.
# Dictionary Methods Example
person = {'name': 'Sohel', 'age': 26, 'city': 'Rangpur'} # Dictionary with key-value pairs
# Output:
print(person.keys()) # dict_keys(['name', 'age', 'city'])
print(person.values()) # dict_values(['Sohel', 26, 'Rangpur'])
print(person.items()) # dict_items([('name', 'Sohel'), ('age', 26), ('city', 'Rangpur')])
# Adding a new key-value pair person.update({'gender': 'Male'})
# Output: print(person) # {'name': 'Sohel', 'age': 26, 'city': 'Rangpur', 'gender': 'Male'}
4. Iterating Through Dictionaries :
- You can iterate through keys, values, or both using methods like keys(), values(), and items().
# Iterating Through a Dictionary Example
person = {'name': 'Ratul', 'age': 26, 'city': 'Cumilla'} # Dictionary with key-value pairs
# Output:
# Iterating through keys for key in person: print(key, person[key]) # Outputs: name Ratul, age 26, city Cumilla
# Iterating through values for value in person.values(): print(value) # Outputs: Ratul, 26, Cumilla
# Iterating through key-value pairs for key, value in person.items():
print(key, value) # Outputs: name Ratul, age 26, city Cumilla
5. Nested Dictionaries :
- Dictionaries can be nested, allowing the representation of complex data structures.
# Nested Dictionaries Example
people = {'person1' : {'name' : 'Fahim' , 'age' : 27, 'city' : 'Barishal'},
'person2': {'name': 'Safin', 'age': 24, 'city': 'Bhola'}}
# Accessing nested values print(people['person1']['name']) # Output: Fahim
print(people['person2']['age']) # Output: 24# Modifying nested valuespeople['person1']['city'] = 'Pirojpur'
print(people['person1']['city']) # Output: Pirojpur
6. Dictionary Comprehensions :
- Similar to lists and sets, dictionary comprehensions provide a concise way to create dictionaries based on existing sequences.
# Dictionary Comprehensions Example
# Creating a dictionary of squares using dictionary comprehensions
squares = {x: x**2 for x in range(1, 7)} # Output: {1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36}# Filtering dictionary items using a condition
even_squares = {x: x**2 for x in range(1, 6) if x % 2 == 0} # Output: {2: 4, 4: 16, 6: 36}