-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdicts.py
70 lines (45 loc) · 1.53 KB
/
dicts.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
64
65
66
67
68
69
70
#####
# DICTIONARIES
#####
# Dicts are in other languages called HashMaps or Associative Arrays
# Every element has a 'key' and a 'value' that are strictly correlated
# The key is unique
# Elements of lists are inside { }
# 'key': 'value' In this case all keys are strings
student = {'name': 'Alessandro', 'age': 21, 'courses': ['Math', 'Algorithms']}
print(student)
print(student['name']) # How to access with key
print(student['courses'])
# print(student['phone']) # KeyError!
print('\n')
# Access
print(student.get('name'))
print(student.get('phone')) # Returns None
print(student.get('phone', 'Not found')) # Returns Not found
print('\n')
# Update
student['phone'] = '123456' # If already exists will override the value
print(student.get('phone'))
student.update({'name': 'Lara', 'age': 26}) # Update values
print(student)
print('\n')
# Delete
del student['phone']
print(student)
age = student.pop('age') # Removes and return the value
print(age)
print(student)
print('\n')
# Other useful methods
print(len(student)) # Number of keys
print(student.keys()) # All Keys
print(student.values()) # All Values
print(student.items()) # All 'Pairs'
print('\n')
student = {'name': 'Alessandro', 'age': 21, 'courses': ['Math', 'Algorithms']}
for key in student: # Loops through the keys
print(key)
print('\n')
for key, value in student.items():
print(key, value) # Loops through keys and values
# --------------------> <-------------------- #