-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDictionary.py
More file actions
93 lines (75 loc) · 2.3 KB
/
Dictionary.py
File metadata and controls
93 lines (75 loc) · 2.3 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# Creating a dictionary
student = {
'name': 'Ali Haider',
'age': 21,
'class': 'BSCS'
}
# Accessing values using keys
print(student)
print(student['name']) # Access value directly by key
print(student.get('name')) # Access value safely using get()
# Updating a value
student['age'] = 22
print(student)
# Looping through dictionary keys and values
for key in student:
print(key, student[key]) # Traditional loop through keys
print('Another method we have:')
for key, value in student.items(): # Recommended way to get both key and value
print(key, value)
# Checking for key existence
if 'mango' in student:
print('Yes, the key "mango" is available') # This will not print, 'mango' not in student
# Dictionary length
print(len(student)) # Total key-value pairs
# Adding a new key-value pair
student['address'] = 'Lahore, Pakistan'
print(student)
# Removing key-value pairs
student.pop('address') # Remove specific key
print(student)
student.popitem() # Remove the most recently added key-value pair
print(student)
del student['name'] # Delete a key-value pair using del
print(student)
# Copying a dictionary
college = student.copy() # Creates a new independent copy
print(college)
# Modifying the copied dictionary
college['location'] = 'Lahore'
print(college) # New key added in copied dictionary
print(student) # Original remains unchanged
# Nested dictionary example
Pakistan = {
'punjab': {
'kasur': {
'pattoki': 'Pattoki',
'habib abad': 'Habib Abad',
'jambar': 'Jamber'
},
'lahore': {
'kanchi': 'Kanchi',
'chungi': 'Chungi',
}
},
'sindh': {
'multan': 'Multan',
'karachi': 'Karachi'
}
}
# Accessing nested dictionary values
print(Pakistan['punjab']['lahore']['kanchi'])
# Dictionary comprehension
square_numbers = {x: x**2 for x in range(10)}
print(square_numbers)
# Clearing a dictionary
square_numbers.clear()
print(square_numbers)
# Creating a dictionary using fromkeys()
default_value = 'taste is very good'
dinner = ['tea', 'patato chips', 'burger']
new_dict = dict.fromkeys(dinner, default_value)
print(new_dict)
# Using setdefault() to add a new key with a default value
default = new_dict.setdefault('age', '21') # Only adds if 'age' doesn't exist
print(new_dict)