-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdictionary_methods.py
63 lines (48 loc) · 1.93 KB
/
dictionary_methods.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# This program demonstrates a representative sample of dictionary methods in Python.
# 1. Creating a dictionary
original_dict = {'name': 'Alice', 'age': 25, 'city': 'New York'}
# 2. Accessing elements
print("Value for 'name':", original_dict['name'])
print("Value for 'age' using get:", original_dict.get('age'))
print("All keys:", original_dict.keys())
print("All values:", original_dict.values())
print("All key-value pairs:", original_dict.items())
# 3. Modifying elements
original_dict['age'] = 26
print("After modification:", original_dict)
# 4. Adding elements
original_dict['gender'] = 'Female'
print("After addition:", original_dict)
# 5. Removing elements
removed_value = original_dict.pop('age')
print("Removed value:", removed_value)
print("After pop:", original_dict)
del original_dict['city']
print("After delete:", original_dict)
original_dict.clear()
print("After clear:", original_dict)
# 6. Copying and updating
shallow_copy = original_dict.copy()
print("Shallow copy:", shallow_copy)
new_dict = {'country': 'USA'}
original_dict.update(new_dict)
print("After update:", original_dict)
# 7. Searching and checking
print("Is 'name' in keys?", 'name' in original_dict)
print("Is 'gender' in keys?", 'gender' in original_dict)
# 8. Getting and setting default values
default_value = original_dict.get('height', 'Not available')
print("Default value for 'height':", default_value)
original_dict.setdefault('height', 170)
print("After setdefault:", original_dict)
# 9. Populating a dictionary using fromkeys
keys = ['a', 'b', 'c']
default_value = 0
new_dict = dict.fromkeys(keys, default_value)
print("Dictionary from keys:", new_dict)
# 10. Merging dictionaries using update (Python 3.9+)
dict1 = {'a': 1, 'b': 2}
dict2 = {'b': 3, 'c': 4}
merged_dict = dict1 | dict2
print("Merged dictionary:", merged_dict)
# Note: This is just a small selection of dictionary methods. Python has many more dictionary methods that you can explore in the documentation.