Python - Dict
** Dict
==========================================
student = {'name' : 'John', 'age' : 25, 'courses': ['Math', 'CompSci']}
print(student)
print(student['name'])
print(student['courses'])
------------------------------------------------------------------
{'name': 'John', 'age': 25, 'courses': ['Math', 'CompSci']}
John
['Math', 'CompSci']
------------------------------------------------------------------
==========================================
student = {'name' : 'John', 'age' : 25, 'courses': ['Math', 'CompSci']}
print(student['phone'])
------------------------------------------------------------------
KeyError: 'phone'
------------------------------------------------------------------
==========================================
student = {'name' : 'John', 'age' : 25, 'courses': ['Math', 'CompSci']}
print(student.get('phone'))
------------------------------------------------------------------
None
------------------------------------------------------------------
==========================================
student = {'name' : 'John', 'age' : 25, 'courses': ['Math', 'CompSci']}
print(student.get('phone', 'Not Found'))
------------------------------------------------------------------
Not Found
------------------------------------------------------------------
==========================================
student = {'name' : 'John', 'age' : 25, 'courses': ['Math', 'CompSci']}
student['phone'] = '555-5555'
student['name'] = 'Jane'
print(student)
------------------------------------------------------------------
{'name': 'Jane', 'age': 25, 'courses': ['Math', 'CompSci'], 'phone': '555-5555'}
------------------------------------------------------------------
==========================================
student = {'name' : 'John', 'age' : 25, 'courses': ['Math', 'CompSci']}
student.update({'name' : 'Jane', 'age' : 26, 'phone' : '555-5555'})
print(student)
------------------------------------------------------------------
{'name': 'Jane', 'age': 26, 'courses': ['Math', 'CompSci'], 'phone': '555-5555'}
------------------------------------------------------------------
==========================================
student = {'name' : 'John', 'age' : 25, 'courses': ['Math', 'CompSci']}
del student['age']
print(student)
------------------------------------------------------------------
{'name': 'John', 'courses': ['Math', 'CompSci']}
------------------------------------------------------------------
==========================================
student = {'name' : 'John', 'age' : 25, 'courses': ['Math', 'CompSci']}
age = student.pop('age')
print(student)
print(age)
------------------------------------------------------------------
{'name': 'John', 'courses': ['Math', 'CompSci']}
25
------------------------------------------------------------------
==========================================
student = {'name' : 'John', 'age' : 25, 'courses': ['Math', 'CompSci']}
print(len(student))
---------------------------------
3
---------------------------------
==========================================
student = {'name' : 'John', 'age' : 25, 'courses': ['Math', 'CompSci']}
print(student.values())
------------------------------------------------------------------
dict_values(['John', 25, ['Math', 'CompSci']])
------------------------------------------------------------------
==========================================
student = {'name' : 'John', 'age' : 25, 'courses': ['Math', 'CompSci']}
print(student.items())
------------------------------------------------------------------
dict_items([('name', 'John'), ('age', 25), ('courses', ['Math', 'CompSci'])])
------------------------------------------------------------------
==========================================
student = {'name' : 'John', 'age' : 25, 'courses': ['Math', 'CompSci']}
for key in student:
print(key)
------------------------------------------------------------------
name
age
courses
------------------------------------------------------------------
==========================================
student = {'name' : 'John', 'age' : 25, 'courses': ['Math', 'CompSci']}
for key, value in student.items():
print(key, value)
------------------------------------------------------------------
name John
age 25
courses ['Math', 'CompSci']
------------------------------------------------------------------