Level 1: Basic Dictionary Creation
Create a simple dictionary with key-value pairs.
# Creating a basic dictionary
person = {
"name": "Alice",
"age": 30,
"city": "New York"
}
print(person)
#Clcoding.com
{'name': 'Alice', 'age': 30, 'city': 'New York'}
Level 2: Accessing and Modifying values
Level 2: Accessing and Modifying Values
Access values using keys, and modify existing key-value pairs.
# Accessing values
print(person["name"])
# Modifying values
person["age"] = 31
print(person["age"])
#Clcoding.com
Alice
31
Level 3: Adding and Removing key Values Pairs
Level 3: Adding and Removing Key-Value Pairs
Add new key-value pairs and remove existing ones.
# Adding a new key-value pair
person["email"] = "alice@example.com"
print(person)
# Removing a key-value pair
del person["city"]
print(person)
#Clcoding.com
{'name': 'Alice', 'age': 31, 'city': 'New York', 'email': 'alice@example.com'}
{'name': 'Alice', 'age': 31, 'email': 'alice@example.com'}
Level 4: Dictionary Methods
Level 4: Dictionary Methods
Level 4: Dictionary Methods
Use dictionary methods like keys(), values(), items(), get(), and pop()
# Getting all keys
print(person.keys())
# Getting all values
print(person.values())
# Getting all key-value pairs
print(person.items())
# Using get() method
print(person.get("name"))
print(person.get("city", "Not Found"))
# Using pop() method
email = person.pop("email")
print(email)
print(person)
dict_keys(['name', 'age', 'email'])
dict_values(['Alice', 31, 'alice@example.com'])
dict_items([('name', 'Alice'), ('age', 31), ('email', 'alice@example.com')])
Alice
Not Found
alice@example.com
{'name': 'Alice', 'age': 31}
Level 5: Dictionary Comprehensions
Level 5: Dictionary Comprehensions
Create dictionaries using dictionary comprehensions for more concise and readable code.
# Dictionary comprehension
squares = {x: x*x for x in range(6)}
print(squares)
#Clcoding.com
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
Level 6: Nested Dictionary
Level 6: Nested Dictionaries
Work with dictionaries within dictionaries to represent more complex data structures.
# Nested dictionary
people = {
"person1": {
"name": "Alice",
"age": 30
},
"person2": {
"name": "Bob",
"age": 25
}
}
print(people)
# Accessing nested dictionary values
print(people["person1"]["name"])
#Clcoding.com
{'person1': {'name': 'Alice', 'age': 30}, 'person2': {'name': 'Bob', 'age': 25}}
Alice
Level 7: Advanced Dictionary Operations
Level 7: Advanced Dictionary Operations
Using advanced features like merging dictionaries, using defaultdict from collections, and performing operations with dict and zip
# Merging dictionaries (Python 3.9+)
dict1 = {"a": 1, "b": 2}
dict2 = {"b": 3, "c": 4}
merged_dict = dict1 | dict2
print(merged_dict) # Output: {'a': 1, 'b': 3, 'c': 4}
# Using defaultdict
from collections import defaultdict
dd = defaultdict(int)
dd["key1"] += 1
print(dd) # Output: defaultdict(<class 'int'>, {'key1': 1})
# Creating a dictionary from two lists using zip
keys = ["name", "age", "city"]
values = ["Charlie", 28, "Los Angeles"]
person = dict(zip(keys, values))
print(person)
#Clcoding.com
{'a': 1, 'b': 3, 'c': 4}
defaultdict(<class 'int'>, {'key1': 1})
{'name': 'Charlie', 'age': 28, 'city': 'Los Angeles'}
0 Comments:
Post a Comment